dcb-service-core-api/src/modules/auth/strategies/api-key.strategy.ts
Mamadou Khoussa [028918 DSI/DAC/DIF/DS] b409b81b18 first commit
2025-10-22 00:58:47 +00:00

26 lines
781 B
TypeScript

import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { HeaderAPIKeyStrategy } from 'passport-headerapikey';
import { PrismaService } from '../../../shared/services/prisma.service';
@Injectable()
export class ApiKeyStrategy extends PassportStrategy(HeaderAPIKeyStrategy, 'api-key') {
constructor(private readonly prisma: PrismaService) {
super(
{ header: 'X-API-Key', prefix: '' },
true
);
}
async validate(apiKey: string, done: any) {
const partner = await this.prisma.partner.findUnique({
where: { apiKey },
});
if (!partner || partner.status !== 'ACTIVE') {
return done(new UnauthorizedException(), false);
}
return done(null, partner);
}
}