Browse Source

fix the change of PassportStrategy from @nestjs/passport

pull/4270/merge^2
HarukaKishida 3 months ago
parent
commit
48659063a2
  1. 58
      apps/api/src/app/auth/api-key.strategy.ts

58
apps/api/src/app/auth/api-key.strategy.ts

@ -21,38 +21,38 @@ export class ApiKeyStrategy extends PassportStrategy(
private readonly prismaService: PrismaService,
private readonly userService: UserService
) {
super(
{ header: HEADER_KEY_TOKEN, prefix: 'Api-Key ' },
true,
async (apiKey: string, done: (error: any, user?: any) => void) => {
try {
const user = await this.validateApiKey(apiKey);
if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
if (hasRole(user, 'INACTIVE')) {
throw new HttpException(
getReasonPhrase(StatusCodes.TOO_MANY_REQUESTS),
StatusCodes.TOO_MANY_REQUESTS
);
}
await this.prismaService.analytics.upsert({
create: { User: { connect: { id: user.id } } },
update: {
activityCount: { increment: 1 },
lastRequestAt: new Date()
},
where: { userId: user.id }
});
}
super({ header: HEADER_KEY_TOKEN, prefix: 'Api-Key ' }, true);
}
done(null, user);
} catch (error) {
done(error, null);
public validate = async (
apiKey: string,
done: (error: any, user?: any) => void
) => {
try {
const user = await this.validateApiKey(apiKey);
if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
if (hasRole(user, 'INACTIVE')) {
throw new HttpException(
getReasonPhrase(StatusCodes.TOO_MANY_REQUESTS),
StatusCodes.TOO_MANY_REQUESTS
);
}
await this.prismaService.analytics.upsert({
create: { User: { connect: { id: user.id } } },
update: {
activityCount: { increment: 1 },
lastRequestAt: new Date()
},
where: { userId: user.id }
});
}
);
}
done(null, user);
} catch (error) {
done(error, null);
}
};
private async validateApiKey(apiKey: string) {
if (!apiKey) {

Loading…
Cancel
Save