import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { UserService } from '../users/user.service' ; import { User } from '../users/user.entity' ; import * as bcrypt from 'bcrypt' ; import { JwtService } from '@nestjs/jwt' ; import { ConfigService } from '@nestjs/config' ; @ Injectable ( ) export class AuthService { constructor ( private readonly userService , private readonly jwtService , private readonly configService ) { } async validateUser ( email , password ) { const user = await this . userService . findByEmail ( email ) ; if ( ! user ) { throw new UnauthorizedException ( 'Username or password is incorrect' ) ; } const compareResult = await bcrypt . compare ( password , user . password ) ; if ( ! compareResult ) { throw new UnauthorizedException ( 'Username or password is incorrect' ) ; } return user ; } async generateJwtToken ( user ) { const payload = { email : user . email , sub : user . id , } ; return { accessToken : await this . jwtService . signAsync ( payload , { expiresIn : this . configService . get < string > ( 'jwtExpiresIn' ) , } ) , } ; } }