Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made health endpoint public #238

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { AuthGuard, IAuthGuard } from '@nestjs/passport';
import { User } from './user.entity';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { Reflector } from '@nestjs/core';
import { IS_PUBLIC_KEY } from './public.decorator';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') implements IAuthGuard {
Expand All @@ -12,6 +14,7 @@ export class JwtAuthGuard extends AuthGuard('jwt') implements IAuthGuard {
constructor(
private readonly configService: ConfigService,
private readonly jwtService: JwtService,
private readonly reflector: Reflector,
) {
super();
this.adminToken = configService.get<string>('ADMIN_TOKEN');
Expand All @@ -23,6 +26,13 @@ export class JwtAuthGuard extends AuthGuard('jwt') implements IAuthGuard {
}

public async canActivate(context: ExecutionContext): Promise<boolean> {
const isPublic = this.reflector.getAllAndOverride<boolean>(
IS_PUBLIC_KEY,
[context.getHandler(), context.getClass()],
);
if (isPublic) {
return true;
}
await super.canActivate(context);
const request: Request = context.switchToHttp().getRequest();
let token = "";
Expand Down
4 changes: 4 additions & 0 deletions src/auth/public.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { SetMetadata } from '@nestjs/common';

export const IS_PUBLIC_KEY = 'isPublic';
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
3 changes: 3 additions & 0 deletions src/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { Body, Controller, Get } from '@nestjs/common';
import { HealthService } from './health.service';
import { HealthCheckResult } from '@nestjs/terminus';
import { Public } from '../auth/public.decorator';

@Controller('health')
export class HealthController {
constructor(
private readonly healthService: HealthService
) {}

@Public()
@Get()
async checkHealth(@Body() body: any): Promise<HealthCheckResult> {
return this.healthService.checkHealth();
}

@Public()
@Get('/ping')
async getServiceHealth(): Promise<HealthCheckResult> {
const resp: HealthCheckResult = {
Expand Down
Loading