0x0x230x

Untitled

Jan 6th, 2026
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1.  
  2. export const createApiKeyGuard = (header: string, getValidKeys: () => string[]): Type<CanActivate> => {
  3. @Injectable()
  4. class ApiKeyGuardMixin implements CanActivate {
  5. canActivate(context: ExecutionContext): boolean {
  6. const request = context.switchToHttp().getRequest();
  7. const apiKey = request.headers[header];
  8.  
  9. if (!apiKey) {
  10. throw new UnauthorizedException();
  11. }
  12.  
  13. if (getValidKeys().includes(apiKey)) {
  14. return true;
  15. }
  16.  
  17. throw new UnauthorizedException();
  18. }
  19. }
  20.  
  21. return mixin(ApiKeyGuardMixin);
  22. };
  23.  
Advertisement
Add Comment
Please, Sign In to add comment