28 lines
733 B
TypeScript
28 lines
733 B
TypeScript
import {
|
|
ExecutionContext,
|
|
Injectable,
|
|
UnauthorizedException
|
|
} from '@nestjs/common'
|
|
import { AuthGuard } from '@nestjs/passport'
|
|
import { Observable } from 'rxjs'
|
|
|
|
@Injectable()
|
|
export class JwtAuthGuard extends AuthGuard('jwt') {
|
|
canActivate(
|
|
context: ExecutionContext
|
|
): boolean | Promise<boolean> | Observable<boolean> {
|
|
// Add your custom authentication logic here
|
|
// for example, call super.logIn(request) to establish a session.
|
|
return super.canActivate(context)
|
|
}
|
|
|
|
handleRequest(err, user, info) {
|
|
// You can throw an exception based on either "info" or "err" arguments
|
|
if (err || !user) {
|
|
throw err || new UnauthorizedException()
|
|
}
|
|
console.log(info)
|
|
return user
|
|
}
|
|
}
|