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