refactor source code

This commit is contained in:
2024-10-29 17:42:54 +07:00
parent 3b23d9e0b7
commit 152bb50da8
83 changed files with 8473 additions and 7577 deletions

View File

@@ -5,9 +5,9 @@ import {
ExecutionContext,
Inject,
UnauthorizedException,
} from '@nestjs/common';
import Clerk from '@clerk/express';
import { GqlExecutionContext } from '@nestjs/graphql';
} from '@nestjs/common'
import Clerk from '@clerk/express'
import { GqlExecutionContext } from '@nestjs/graphql'
@Injectable()
export class ClerkAuthGuard implements CanActivate {
@@ -15,36 +15,36 @@ export class ClerkAuthGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
// For GraphQL, get the request from the GQL context
const ctx = GqlExecutionContext.create(context);
const request = ctx.getContext().req;
const ctx = GqlExecutionContext.create(context)
const request = ctx.getContext().req
// Get the token from the Authorization header
const authHeader = request.headers['authorization'];
const authHeader = request.headers['authorization']
if (!authHeader) {
throw new UnauthorizedException('Authorization header not found');
throw new UnauthorizedException('Authorization header not found')
}
const token = authHeader.split(' ')[1]; // Assuming 'Bearer TOKEN'
const token = authHeader.split(' ')[1] // Assuming 'Bearer TOKEN'
if (!token) {
throw new UnauthorizedException('Token not found');
throw new UnauthorizedException('Token not found')
}
try {
// Verify the token with Clerk
const session = await this.clerk.verifyToken(token, {});
const session = await this.clerk.verifyToken(token, {})
if (!session) {
throw new UnauthorizedException('Invalid session');
throw new UnauthorizedException('Invalid session')
}
// Attach user info to the request context if needed
request.user = session.user;
request.user = session.user
return true;
return true
} catch (error: any) {
throw new UnauthorizedException(error.message);
throw new UnauthorizedException(error.message)
}
}
}