Skip to content

Instantly share code, notes, and snippets.

@dipzera
Created May 3, 2022 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dipzera/684a99744c3393931c56e5474b2162a7 to your computer and use it in GitHub Desktop.
Save dipzera/684a99744c3393931c56e5474b2162a7 to your computer and use it in GitHub Desktop.
async logout(userId: string): Promise<void> {
await this.prisma.user.update({
where: {
id: userId,
},
data: {
hashedRefreshToken: null
},
});
}
async refresh(userId: string, refreshToken: string): Promise<Tokens> {
const user = await this.prisma.user.findUnique({
where: {
id: userId,
},
});
if (!user) {
throw new ForbiddenException('Access denied');
}
const tokenMatches = await bcrypt.compare(refreshToken, user.hashedRefreshToken);
// The token is invalid and we can't issue a pair of new tokens
if (!tokenMatches) {
throw new ForbiddenException('Access denied');
}
const tokens = this.signTokens(user.id, user.email);
await this.updateRefreshToken(user.id, tokens.refreshToken);
return tokens;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment