1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37import {
Injectable,
ExecutionContext,
UnauthorizedException,
Logger,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
constructor(private readonly jwtService: JwtService) {
super();
}
async canActivate(context: ExecutionContext) {
const request = context.switchToHttp().getRequest();
const authHeader = request.headers.authorization;
if (!authHeader) {
throw new UnauthorizedException('Authorization ํค๋๊ฐ ์์ต๋๋ค.');
}
const token = authHeader.split(' ')[1];
if (!token) {
throw new UnauthorizedException('ํ ํฐ์ด ์กด์ฌํ์ง ์์ต๋๋ค.');
}
try {
const decoded = this.jwtService.verify(token);
request.user = decoded;
return true;
} catch (err) {
throw new UnauthorizedException('์ ํจํ์ง ์์ ํ ํฐ์
๋๋ค.');
}
}
}