Skip to content

Instantly share code, notes, and snippets.

@dipzera
Created May 3, 2022 17:39
Show Gist options
  • Save dipzera/9c27658314437f2066eb7050d7dcd83b to your computer and use it in GitHub Desktop.
Save dipzera/9c27658314437f2066eb7050d7dcd83b to your computer and use it in GitHub Desktop.
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