Refactor code structure and dependencies
This commit is contained in:
412
package-lock.json
generated
412
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
50
src/clerk/clerk-auth.guard.ts
Normal file
50
src/clerk/clerk-auth.guard.ts
Normal 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
14
src/clerk/clerk.module.ts
Normal 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 {}
|
||||
@@ -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, {});
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {}
|
||||
export class UsersService {
|
||||
async findOneById(id: string) {
|
||||
return { id };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user