40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
![]() |
import {
|
||
|
CanActivate,
|
||
|
ExecutionContext,
|
||
|
Injectable,
|
||
|
UnauthorizedException,
|
||
|
} from '@nestjs/common';
|
||
|
import { JwtService } from '@nestjs/jwt';
|
||
|
import { env } from '@server/env';
|
||
|
import { Request } from 'express';
|
||
|
|
||
|
@Injectable()
|
||
|
export class AuthGuard implements CanActivate {
|
||
|
constructor(private jwtService: JwtService) { }
|
||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||
|
const request = context.switchToHttp().getRequest();
|
||
|
const token = this.extractTokenFromHeader(request);
|
||
|
if (!token) {
|
||
|
throw new UnauthorizedException();
|
||
|
}
|
||
|
try {
|
||
|
const payload = await this.jwtService.verifyAsync(
|
||
|
token,
|
||
|
{
|
||
|
secret: env.JWT_SECRET
|
||
|
}
|
||
|
);
|
||
|
// 💡 We're assigning the payload to the request object here
|
||
|
// so that we can access it in our route handlers
|
||
|
request['user'] = payload;
|
||
|
} catch {
|
||
|
throw new UnauthorizedException();
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
private extractTokenFromHeader(request: Request): string | undefined {
|
||
|
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
||
|
return type === 'Bearer' ? token : undefined;
|
||
|
}
|
||
|
}
|