Здравствуйте, я получил ошибку 404, не найденную, как указано на снимке экрана ниже. Я использую правильный путь, но все еще получил ошибку
Вот код UsersService:
constructor(@Inject('UserModelToken') private readonly userModel: Model<User>) { }
async findAll(): Promise<User[]> {
return await this.userModel.find().exec();
}
async findOne(payload: JwtPayload): Promise<User[]> {
return await this.userModel.findOne({ username: payload.username }).exec();
}
async create(createUserDto: CreateUserDto): Promise<User> {
//createUserDto.password = await this.getHash(createUserDto.password);
const createdUser = new this.userModel(createUserDto);
return await createdUser.save();
}
}
Вот код AuthService:
constructor(private readonly jwtService: JwtService, private readonly usersService: UsersService) { }
async createToken(user: JwtPayload) {
// const user: JwtPayload = { email: 'test@email.com' };
const accessToken = this.jwtService.sign(user);
return {
expiresIn: 3600,
accessToken,
};
}
async validateUser(payload: JwtPayload): Promise<any> {
// Validate if token passed along with HTTP request
// is associated with any registered account in the database
return await this.usersService.findOne(payload);
}
}
Вот код AuthController:
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService, private readonly usersService: UsersService) { }
@Post('login')
async login(@Body() payload: JwtPayload) {
const user = await this.usersService.findOne(payload);
if (user) {
//if (await this.usersService.compareHash(payload.password, user['password'])) {
return await this.authService.createToken(payload);
}
else {
throw new HttpException({
status: HttpStatus.UNAUTHORIZED,
error: 'Wrong username or password',
}, HttpStatus.UNAUTHORIZED);
}
}
Вот код DTO:
import {IsString, IsInt,IsEmail,IsNotEmpty, IsNumberString, IsIn,IsHash} from 'class-validator'
export class CreateUserDto{
@IsEmail()
username:string
@IsNotEmpty()
password:string
}