Skip to content

Commit

Permalink
fix: public decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Jun 18, 2024
1 parent e0f02f5 commit ca59e8f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
8 changes: 5 additions & 3 deletions apps/service-auth/src/clans/clans.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CurrentUser } from '@app/auth';
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { CurrentUser, JwtAuthGuard, RolesGuard } from '@app/auth';
import { Public } from '@app/auth/decorators';
import { Controller, Get, Param, Query, Res, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { ClansService } from './clans.service';
Expand All @@ -10,7 +11,7 @@ import { SeasonInput } from './dto/season-input.dto';
@ApiTags('CLANS')
@ApiBearerAuth()
@Controller('/clans')
// @UseGuards(JwtAuthGuard, RolesGuard)
@UseGuards(JwtAuthGuard, RolesGuard)
export class ClansController {
constructor(private clansService: ClansService) {}

Expand Down Expand Up @@ -40,6 +41,7 @@ export class ClansController {
return this.clansService.getCWLStats(clanTag);
}

@Public()
@Get('/:clanTag/badges/:size')
@ApiResponse({ type: CWLStatsOutput, status: 200 })
async getClanBadges(
Expand Down
1 change: 0 additions & 1 deletion apps/service-auth/src/clans/clans.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ export class ClansService {

public async getClanBadges(clanTag: string, size: string) {
const clan = await this.clashClientService.getClanOrThrow(clanTag);
console.log(clan.badgeUrls[size]);
return fetch(clan.badgeUrls[size]).then((res) => res.arrayBuffer());
}
}
1 change: 1 addition & 0 deletions libs/auth/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './current-user.decorator';
export * from './roles.decorator';
export * from './public.decorator';
5 changes: 5 additions & 0 deletions libs/auth/src/decorators/public.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SetMetadata } from '@nestjs/common';

export const IS_PUBLIC_KEY = 'isPublic';

export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
22 changes: 20 additions & 2 deletions libs/auth/src/guards/jwt-auth.guards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { Injectable } from '@nestjs/common';
import { ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard } from '@nestjs/passport';
import { AuthGuardStrategyMapping } from 'apps/service-auth/src/app.constants';
import { Observable } from 'rxjs';
import { IS_PUBLIC_KEY } from '../decorators/public.decorator';

@Injectable()
export class JwtAuthGuard extends AuthGuard(AuthGuardStrategyMapping.JWT) {}
export class JwtAuthGuard extends AuthGuard(AuthGuardStrategyMapping.JWT) {
public constructor(private reflector: Reflector) {
super();
}

canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass(),
]);

if (isPublic) return true;

return super.canActivate(context);
}
}

0 comments on commit ca59e8f

Please sign in to comment.