Refactor code structure and dependencies

This commit is contained in:
2024-09-29 22:52:30 +07:00
parent 88176bddc1
commit 7ce33c6c3a
8 changed files with 499 additions and 32 deletions

412
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -24,6 +24,7 @@
},
"dependencies": {
"@apollo/server": "^4.11.0",
"@clerk/clerk-sdk-node": "^5.0.45",
"@graphql-codegen/cli": "^5.0.2",
"@graphql-codegen/introspection": "^4.0.3",
"@graphql-codegen/typescript": "^4.0.9",
@@ -55,6 +56,10 @@
"rxjs": "^7.8.1"
},
"devDependencies": {
"@clerk/types": "^4.23.0",
"@graphql-codegen/cli": "5.0.2",
"@graphql-codegen/typescript": "4.0.9",
"@graphql-codegen/typescript-resolvers": "4.2.1",
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
@@ -80,10 +85,7 @@
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3",
"ws": "^8.18.0",
"@graphql-codegen/typescript-resolvers": "4.2.1",
"@graphql-codegen/typescript": "4.0.9",
"@graphql-codegen/cli": "5.0.2"
"ws": "^8.18.0"
},
"jest": {
"moduleFileExtensions": [
@@ -102,4 +104,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}

View File

@@ -2,10 +2,12 @@ import { Module } from '@nestjs/common';
import { GraphqlModule } from './graphql/graphql.module';
import { PrismaModule } from './prisma/prisma.module';
import { UsersModule } from './users/users.module';
import { ClerkModule } from './clerk/clerk.module';
@Module({
imports: [
GraphqlModule, // GraphQL setup
ClerkModule, // Clerk setup
],
})
export class AppModule {}

View File

@@ -0,0 +1,50 @@
// clerk-auth.guard.ts
import {
Injectable,
CanActivate,
ExecutionContext,
Inject,
UnauthorizedException,
} from '@nestjs/common';
import Clerk from '@clerk/clerk-sdk-node';
import { GqlExecutionContext } from '@nestjs/graphql';
@Injectable()
export class ClerkAuthGuard implements CanActivate {
constructor(@Inject('CLERK') private readonly clerk: typeof Clerk) {}
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;
// Get the token from the Authorization header
const authHeader = request.headers['authorization'];
if (!authHeader) {
throw new UnauthorizedException('Authorization header not found');
}
const token = authHeader.split(' ')[1]; // Assuming 'Bearer TOKEN'
if (!token) {
throw new UnauthorizedException('Token not found');
}
try {
// Verify the token with Clerk
const session = await this.clerk.verifyToken(token, {});
if (!session) {
throw new UnauthorizedException('Invalid session');
}
// Attach user info to the request context if needed
request.user = session.user;
return true;
} catch (error: any) {
throw new UnauthorizedException(error.message);
}
}
}

14
src/clerk/clerk.module.ts Normal file
View File

@@ -0,0 +1,14 @@
import { Module, Global } from '@nestjs/common';
import Clerk from '@clerk/clerk-sdk-node';
@Global()
@Module({
providers: [
{
provide: 'CLERK',
useValue: Clerk,
},
],
exports: ['CLERK'],
})
export class ClerkModule {}

View File

@@ -6,14 +6,17 @@ import { PrismaService } from '../prisma/prisma.service';
import type PrismaTypes from '../types/pothos.generated';
import { getDatamodel } from '../types/pothos.generated';
export const prisma = new PrismaService({});
export const prisma = new PrismaService({
log: ['query', 'info', 'warn', 'error'],
errorFormat: 'pretty',
});
export const builder = new SchemaBuilder<{
Scalars: {
DateTime: {
Input: Date;
Output: Date;
};
Scalars: {
DateTime: {
Input: Date;
Output: Date;
};
};
PrismaTypes: PrismaTypes;
}>({
plugins: [PrismaPlugin, PrismaUtils],
@@ -25,4 +28,5 @@ export const builder = new SchemaBuilder<{
dmmf: getDatamodel(),
},
});
builder.addScalarType('DateTime', DateTimeResolver, {});

View File

@@ -1,18 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from './prisma.service';
describe('PrismaService', () => {
let service: PrismaService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PrismaService],
}).compile();
service = module.get<PrismaService>(PrismaService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -1,4 +1,7 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class UsersService {}
export class UsersService {
async findOneById(id: string) {
return { id };
}
}