expose is active in service

This commit is contained in:
2024-10-27 14:10:18 +07:00
parent 9e0e97a560
commit 2369275237
4 changed files with 54 additions and 5 deletions

View File

@@ -1,4 +1,41 @@
import { Injectable } from '@nestjs/common';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PrismaService } from '../Prisma/prisma.service';
import { Request } from 'express';
import { clerkClient } from '@clerk/express';
@Injectable()
export class GraphqlService {}
export class GraphqlService {
constructor(private readonly prisma: PrismaService) {}
async acquireContext(req: Request) {
// development flag
const isDevelopment = process.env.NODE_ENV === 'development';
if (isDevelopment) {
return null;
}
// get x-session-id from headers
let sessionId;
try {
sessionId = req.headers['x-session-id'];
//eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
throw new UnauthorizedException('Must provide a session ID');
}
if (!sessionId) {
throw new UnauthorizedException('Session ID is required');
}
// check if the token is valid
const session = await clerkClient.sessions.getSession(sessionId as string);
if (!session) {
throw new UnauthorizedException('Invalid session');
}
const user = await this.prisma.user.findUnique({
where: { id: session.userId },
});
if (!user) {
throw new UnauthorizedException('User not found');
}
return user;
}
}