import { Injectable, UnauthorizedException } from "@nestjs/common"; import { PassportStrategy } from "@nestjs/passport"; import { ExtractJwt, Strategy } from "passport-jwt"; import { ConfigService } from "@nestjs/config"; import { PrismaService } from "../prisma/prisma.service"; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(config: ConfigService, private prisma: PrismaService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: config.get("JWT_SECRET", "change-me"), }); } async validate(payload: { sub: string; email: string; role: string; tenantId: string }): Promise { const user = await this.prisma.client.user.findUnique({ where: { id: payload.sub } }); if (!user || !user.isActive) throw new UnauthorizedException(); return { id: user.id, email: user.email, role: user.role, tenantId: user.tenantId }; } }