import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { JwtService } from '@nestjs/jwt' ; import { Hash } from '../../utils/Hash' ; import { ConfigService } from './../config' ; import { User , UsersService } from './../user' ; import { LoginPayload } from './login.payload' ; @ Injectable ( ) export class AuthService { constructor ( private readonly jwtService , private readonly configService , private readonly userService ) { } async createToken ( user ) { return { expiresIn : this . configService . get ( 'JWT_EXPIRATION_TIME' ) , accessToken : this . jwtService . sign ( { id : user . id } ) , user , } ; } async validateUser ( payload ) { const user = await this . userService . getByEmail ( payload . email ) ; if ( ! user || ! Hash . compare ( payload . password , user . password ) ) { throw new UnauthorizedException ( 'Invalid credentials!' ) ; } return user ; } }