-
-
Save dipzera/9c27658314437f2066eb7050d7dcd83b to your computer and use it in GitHub Desktop.
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 * as bcrypt from 'bcrypt'; | |
import { Tokens } from './auth.controller.ts'; | |
@Injectable() | |
export class AuthService { | |
constructor( | |
private prisma: PrismaService, | |
private jwtService: JwtService | |
) {} | |
private async signTokens(userId: string, email: string): Promise<Tokens> { | |
const refreshToken = await this.jwtService | |
.signAsync( | |
{ sub: userId, email, }, | |
{ expiresIn: this.configService.get('REFRESH_LIFESPAN') } | |
); | |
const accessToken = await this.jwtService | |
.signAsync( | |
{ sub: userId, email, }, | |
{ expiresIn: this.configService.get('ACCESS_LIFESPAN') } | |
); | |
return { | |
refreshToken, | |
accessToken, | |
} | |
} | |
private async updateRefreshToken( | |
userId: string, | |
refreshToken: string | |
): Promise<void> { | |
const hashedRefreshToken = await this.bcrypt.hash(refreshToken); | |
await this.prisma.update({ | |
where: { | |
id: userId, | |
}, | |
data: { | |
hashedRefreshToken | |
}, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment