import { Injectable , Logger } from '@nestjs/common' ; import { UserService } from '../user/user.service' ; import { JwtPayload } from './jwt-payload' ; import { TokenService } from './token/token.service' ; import { LoginResponseVm } from './view-models/login-response-vm.model' ; import { LoginVm } from './view-models/login-vm.model' ; @ Injectable ( ) export class AuthService { private readonly logger = new Logger ( 'AuthService' ) ; constructor ( private readonly tokenService , private readonly userService ) { } async login ( credentials , ipAddress ) { const loginResults = await this . userService . login ( credentials ) ; if ( ! loginResults ) { return null ; } const payload = { sub : loginResults . id , } ; const loginResponse = await this . tokenService . createAccessToken ( payload ) ; const tokenContent = { userId : loginResults . id , clientId : credentials . clientId , ipAddress , } ; const refresh = await this . tokenService . createRefreshToken ( tokenContent ) ; loginResponse . refreshToken = refresh ; return loginResponse ; } async logout ( userId , refreshToken ) { await this . tokenService . deleteRefreshToken ( userId , refreshToken ) ; } param userId async logoutFromAll ( userId ) { await this . tokenService . deleteRefreshTokenForUser ( userId ) ; } }