Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const createApiKeyGuard = (header: string, getValidKeys: () => string[]): Type<CanActivate> => {
- @Injectable()
- class ApiKeyGuardMixin implements CanActivate {
- canActivate(context: ExecutionContext): boolean {
- const request = context.switchToHttp().getRequest();
- const apiKey = request.headers[header];
- if (!apiKey) {
- throw new UnauthorizedException();
- }
- if (getValidKeys().includes(apiKey)) {
- return true;
- }
- throw new UnauthorizedException();
- }
- }
- return mixin(ApiKeyGuardMixin);
- };
Advertisement
Add Comment
Please, Sign In to add comment