JwtAuthGuard
This commit is contained in:
parent
a00990b6d6
commit
19452bb01e
@ -2,14 +2,18 @@ import { Reflector } from '@nestjs/core'
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
Logger,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common'
|
||||
import { AuthGuard } from '@nestjs/passport'
|
||||
import 'rxjs'
|
||||
import { NEED_AUTH_KEY } from '../decorators/need-auth.decorator'
|
||||
import { JsonWebTokenError } from 'jsonwebtoken'
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
private readonly logger = new Logger(JsonWebTokenError.name)
|
||||
|
||||
constructor(private reflector: Reflector) {
|
||||
super()
|
||||
}
|
||||
@ -25,10 +29,10 @@ export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
return true
|
||||
}
|
||||
|
||||
handleRequest(err, user, info) {
|
||||
// You can throw an exception based on either "info" or "err" arguments
|
||||
if (err || !user) {
|
||||
throw err || new UnauthorizedException()
|
||||
handleRequest(err: Error, user, info: Error) {
|
||||
if (err || info || !user) {
|
||||
this.logger.error(err?.message || info?.message)
|
||||
throw new UnauthorizedException(err?.message || info?.message)
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user