feat: Add error handling for invalid token in JwtAuthGuard
This commit is contained in:
parent
80ce7d7f16
commit
28e42b2248
@ -47,9 +47,13 @@ export class JwtAuthGuard implements CanActivate {
|
||||
algorithms: ["RS256"],
|
||||
});
|
||||
|
||||
let user = await this.userService.findByProviderId(
|
||||
verifiedToken.sub.toString(),
|
||||
);
|
||||
if (typeof verifiedToken == "string")
|
||||
throw new UnauthorizedException("Invalid token");
|
||||
|
||||
let user = await this.userService.findOrCreateByProviderId({
|
||||
providerId: verifiedToken.sub.toString(),
|
||||
username: verifiedToken.preferred_username,
|
||||
});
|
||||
|
||||
request.user = user;
|
||||
|
||||
|
@ -45,6 +45,31 @@ export class UserService {
|
||||
});
|
||||
}
|
||||
|
||||
async findOrCreateByProviderId({
|
||||
providerId,
|
||||
username,
|
||||
}: {
|
||||
providerId: string;
|
||||
username: string;
|
||||
}) {
|
||||
let user = await this.prisma.user.findUnique({
|
||||
where: {
|
||||
providerId,
|
||||
username,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user)
|
||||
user = await this.prisma.user.create({
|
||||
data: {
|
||||
providerId,
|
||||
username,
|
||||
},
|
||||
});
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async create(createUserDto: CreateUserDTO) {
|
||||
return await this.prisma.user.create({
|
||||
data: {
|
||||
|
Loading…
Reference in New Issue
Block a user