diff --git a/src/auth/google/google.guard.ts b/src/auth/guard/google.guard.ts similarity index 100% rename from src/auth/google/google.guard.ts rename to src/auth/guard/google.guard.ts diff --git a/src/auth/jwt/jwt.guard.ts b/src/auth/guard/jwt.guard.ts similarity index 51% rename from src/auth/jwt/jwt.guard.ts rename to src/auth/guard/jwt.guard.ts index ced9c59..7ba18a7 100644 --- a/src/auth/jwt/jwt.guard.ts +++ b/src/auth/guard/jwt.guard.ts @@ -1,28 +1,48 @@ import { Injectable, ExecutionContext, Logger } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; +import { JwtService } from '@nestjs/jwt'; @Injectable() export class JwtGuard extends AuthGuard('jwt') { private readonly logger = new Logger(JwtGuard.name); + constructor(private readonly jwtService: JwtService) { + super(); + } - async canActivate(context: ExecutionContext): Promise{ + async canActivate(context: ExecutionContext): Promise { if (context.getType() === 'http') { - this.logger.debug(`canActivate [http, ${context.getArgs()[0].method} ${context.getArgs()[0].url}]`); - } else{ + this.logger.debug( + `canActivate [http, ${context.getArgs()[0].method} ${ + context.getArgs()[0].url + }]`, + ); + } else { this.logger.debug(`canActivate [not http]`); return false; } - const token = context.switchToHttp().getRequest().headers?.authorization?.split('Bearer ')[1]; + const token = context + .switchToHttp() + .getRequest() + .headers?.authorization?.split('Bearer ')[1]; this.logger.debug(`canActivate [token: ${token}]`); + // if (!token) { + // this.logger.debug(`canActivate [no token]`); + // return false; + // } let result: boolean = false; try { result = (await super.canActivate(context)) as boolean; this.logger.debug(`canActivate [result: ${result}]`); } catch (error) { this.logger.error(`canActivate [error: ${error}]`); - result = false; + /* + token could be expired or invaild + return exception to client and let client to refresh token + */ + throw error; } + this.logger.debug(`canActivate [result: ${result}]`); return result; } -} \ No newline at end of file +} diff --git a/src/auth/role/role.guard.ts b/src/auth/guard/role.guard.ts similarity index 100% rename from src/auth/role/role.guard.ts rename to src/auth/guard/role.guard.ts diff --git a/src/auth/google/google.strategy.ts b/src/auth/strategy/google.strategy.ts similarity index 100% rename from src/auth/google/google.strategy.ts rename to src/auth/strategy/google.strategy.ts diff --git a/src/auth/jwt/jwt.strategy.ts b/src/auth/strategy/jwt.strategy.ts similarity index 92% rename from src/auth/jwt/jwt.strategy.ts rename to src/auth/strategy/jwt.strategy.ts index 513b413..d3cee0b 100644 --- a/src/auth/jwt/jwt.strategy.ts +++ b/src/auth/strategy/jwt.strategy.ts @@ -7,9 +7,7 @@ import { VerifyCallback } from 'jsonwebtoken'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { private readonly logger = new Logger(JwtStrategy.name); - constructor ( - private readonly configService: ConfigService, - ){ + constructor(private readonly configService: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: configService.get('jwt.secret'), @@ -25,4 +23,4 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { throw new UnauthorizedException('Unauthorized', '401'); } } -} \ No newline at end of file +} diff --git a/src/auth/google/google.middleware.ts b/src/middleware/google.middleware.ts similarity index 100% rename from src/auth/google/google.middleware.ts rename to src/middleware/google.middleware.ts