import { User } from '@/users/users.model' ; import { UsersService } from '@/users/users.service' ; import { Injectable } from '@nestjs/common' ; import { JwtService } from '@nestjs/jwt' ; import { encryptValue } from '@utils/crypto' ; import { TokenUser } from '@decorators/user.decorator' ; @ Injectable ( ) export class AuthService { constructor ( private userService , private jwtService ) { } async validateUser ( userId , password ) { const user = await this . userService . getByUserId ( userId ) ; const isCompared = await user ?. comparePassword ( password ) ; if ( ! isCompared || ! user ) return null ; return { id : user . id } ; } async signin ( payload ) { const accessToken = this . jwtService . sign ( payload , { expiresIn : '15m' } ) ; const refreshToken = this . jwtService . sign ( payload , { expiresIn : '14d' } ) ; await this . userService . updateRefreshToken ( payload . id , refreshToken ) ; return encryptValue ( accessToken ) ; } async verifyRefresh ( id ) { const user = await this . userService . getById ( id ) ; if ( ! user ) return false ; return user . verifyRefresh ( ) ; } }