import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { APP__ACCESS_TOKEN } from '../models/commons/consts/env.variables'; @Injectable() export class AccessTokenGuard implements CanActivate { constructor(private readonly _configService: ConfigService) {} async canActivate(context: ExecutionContext): Promise { try { const req = context.switchToHttp().getRequest(); const token = req.headers['x-access-token']; if (!token || token !== this._configService.get(APP__ACCESS_TOKEN)) { throw new UnauthorizedException(); } return Promise.resolve(true); } catch (e) { return Promise.reject(e); } } }