-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathjwt.strategy.ts
executable file
·32 lines (29 loc) · 966 Bytes
/
jwt.strategy.ts
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
/**
* @file Auth jwt strategy
* @module module/auth/jwt-strategy
* @author Surmon <https://github.com/surmon-china>
*/
// https://docs.nestjs.com/security/authentication#implementing-passport-jwt
import { Injectable } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport'
import { ExtractJwt, Strategy } from 'passport-jwt'
import { HttpUnauthorizedError } from '@app/errors/unauthorized.error'
import { AuthService } from './auth.service'
import * as APP_CONFIG from '@app/app.config'
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: APP_CONFIG.AUTH.jwtSecret
})
}
validate(payload: any) {
const data = this.authService.validateAuthData(payload)
if (data) {
return data
} else {
throw new HttpUnauthorizedError()
}
}
}