Created
March 4, 2019 15:15
-
-
Save saurabhpati/07e5b67c573297d2261b292206e05439 to your computer and use it in GitHub Desktop.
Auth Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@nestjs/common'; | |
import { JwtService } from '@nestjs/jwt'; | |
import { JwtPayload } from './interfaces/jwt-payload.interface'; | |
import { UserDto } from './dto/user.dto'; | |
import { UserService } from '../user/user.service'; | |
@Injectable() | |
export class AuthService { | |
constructor( | |
private readonly jwtService: JwtService, | |
private readonly userService: UserService) { | |
} | |
private async createToken(user: JwtPayload) { | |
const accessToken = this.jwtService.sign(user); | |
return { | |
expiresIn: 3600, | |
accessToken, | |
}; | |
} | |
async validateUser(userDto: UserDto): Promise<any> { | |
const isValid = !!(await this.userService.get(userDto.username)); | |
return isValid ? this.createToken(userDto) : new Promise((resolve, reject) => { | |
resolve({ | |
expiresIn: null, | |
accessToken: null | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment