-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJWT.strategy.ts
More file actions
38 lines (31 loc) · 1.07 KB
/
JWT.strategy.ts
File metadata and controls
38 lines (31 loc) · 1.07 KB
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
37
38
import { Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import type { Request } from 'express';
import { ExtractJwt, Strategy } from 'passport-jwt';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
private static logger = new Logger(JwtStrategy.name);
constructor(@Inject(ConfigService) config: ConfigService) {
const JWT_SECRET = config.getOrThrow('JWT_SECRET');
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: JWT_SECRET,
passReqToCallback: true,
});
}
public validate(req: Request, payload: any) {
const refreshTokenHeader = req.headers?.authorization;
const refreshTokenCookie = req.cookies?.refresh_token;
const refreshToken = refreshTokenHeader
? refreshTokenHeader.split(' ')[1]
: refreshTokenCookie;
if (!refreshToken) {
throw new Error('No refresh token');
}
return {
...payload,
refreshToken,
};
}
}