Skip to content

Commit

Permalink
feat(nestjs-auth): add log to guard (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsangste authored Sep 12, 2024
1 parent 27fa2e2 commit 3f31eb0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions libs/nestjs-auth/src/guards/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { Reflector } from '@nestjs/core'
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'
import {
CanActivate,
ExecutionContext,
Inject,
Injectable,
Logger,
LoggerService,
UnauthorizedException
} from '@nestjs/common'

import { Request } from 'express'
import { Observable } from 'rxjs'

@Injectable()
export class AuthGuard implements CanActivate {
constructor(private readonly reflector: Reflector) {}
constructor(@Inject(Logger) private readonly logger: LoggerService, private readonly reflector: Reflector) {}

canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const allowUnauthorizedRequest =
Expand All @@ -17,6 +25,7 @@ export class AuthGuard implements CanActivate {
const user = request.user

if (!allowUnauthorizedRequest && !user) {
this.logger.warn('unauthorized user')
throw new UnauthorizedException()
}

Expand Down

0 comments on commit 3f31eb0

Please sign in to comment.