import { Injectable } from '@nestjs/common' ; import * as bcrypt from 'bcrypt' ; import { JwtService } from '@nestjs/jwt' ; import { UserService } from '../user/user.service' ; import { User } from '../user/entities/user.entity' ; import { jwtConstants } from './constants' ; @ Injectable ( ) export class AuthService { constructor ( private userService , private jwtService ) { } async validateUser ( username , password ) { const existUser = await this . userService . findByUsername ( username ) ; if ( ! existUser ) { return null ; } const isMatch = await bcrypt . compare ( password , existUser . password ) ; if ( ! isMatch ) { return null ; } const { password : ignorePass , ... restUser } = existUser ; return restUser ; } async login ( user ) { const { password , ... restUser } = user ; const payload = { ... restUser , sub : user . id } ; return { token : this . jwtService . sign ( payload ) , user : restUser , expiresIn : jwtConstants . expiresIn , } ; } }