[Nest] 19432 - 06.05.2023 23:05:18 ERROR [ExceptionsHandler] user.checkPassword is not a function
TypeError: user.checkPassword is not a function
at UserService.findByEmailAndPassword (C:\Users\muham\AwesomeProject\back-end\src\users\user.service.ts:48:33)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at AuthController.login (C:\Users\muham\AwesomeProject\back-end\src\auth.controller.ts:12:22)
at C:\Users\muham\AwesomeProject\back-end\node_modules\@nestjs\core\router\router-execution-context.js:46:28
at C:\Users\muham\AwesomeProject\back-end\node_modules\@nestjs\core\router\router-proxy.js:9:17
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import * as bcrypt from 'bcrypt';
@Schema()
export class User extends Document {
@Prop({ required: true, unique: true })
email: string;
@Prop({ required: true })
password: string;
@Prop({ required: true })
name: string;
@Prop({ required: true })
location: string;
@Prop([String])
preferredSports: string[];
@Prop([String])
messages: string[];
@Prop({ default: 0 })
rating: number;
//check if the password is valid
async checkPassword(attempt: string): Promise<boolean> {
return await bcrypt.compare(attempt, this.password);
}
}
export const UserSchema = SchemaFactory.createForClass(User);
UserSchema.index({ email: 1 }, { unique: true });
export type UserDocument = User & Document;
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from './schemas/user.schema';
import { UpdateUserDto, CreateUserDto } from './dto';
@Injectable()
export class UserService {
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}
async create(createUserDto: CreateUserDto): Promise<User> {
const createdUser = new this.userModel(createUserDto);
return createdUser.save();
}
async findAll(): Promise<User[]> {
return this.userModel.find().exec();
}
async findOne(id: string): Promise<User> {
return this.userModel.findById(id).exec();
}
async findByEmail(email: string): Promise<UserDocument> {
return this.userModel.findOne({ email }).exec();
}
async findById(id: string): Promise<UserDocument> {
return this.userModel.findById(id).exec();
}
async update(id: string, updateUserDto: UpdateUserDto): Promise<UserDocument> {
return this.userModel.findByIdAndUpdate(id, updateUserDto, { new: true }).exec();
}
async delete(id: string): Promise<UserDocument> {
return this.userModel.findByIdAndDelete(id).exec();
}
async findByName(name: string): Promise<UserDocument> {
return this.userModel.findOne({ name }).exec();
}
async findByEmailAndPassword(email: string, password: string): Promise<UserDocument> {
const user = await this.findByEmail(email);
console.log(user);
if (user && (await user.checkPassword(password))) {
return user;
}
return null;
}
}
// auth.controller.ts
import { Controller, Post, Body, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
@Post('login')
async login(@Body() loginData: { email: string; password: string }) {
const user = await this.authService.validateUser(loginData.email, loginData.password);
if (user) {
const accessToken = await this.authService.login(user);
return { accessToken };
}
throw new UnauthorizedException('Invalid email or password');
}
}
i need to checkpassword