-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
461 additions
and
366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
|
||
import { AppController } from './app.controller'; | ||
import { AppController } from './app.controller' | ||
|
||
describe('AppController', () => { | ||
let app: TestingModule; | ||
let app: TestingModule | ||
|
||
beforeAll(async () => { | ||
app = await Test.createTestingModule({ | ||
controllers: [AppController], | ||
providers: [], | ||
}).compile(); | ||
}); | ||
providers: [] | ||
}).compile() | ||
}) | ||
|
||
describe('healthCheck', () => { | ||
it('should return "Hello API"', () => { | ||
const appController = app.get<AppController>(AppController); | ||
expect(appController.health()).toEqual('UP'); | ||
}); | ||
}); | ||
}); | ||
const appController = app.get<AppController>(AppController) | ||
expect(appController.health()).toEqual('UP') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { Controller, Get } from '@nestjs/common' | ||
import { Public } from '../decorators/public.decorator' | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor() {} | ||
|
||
@Get('health') | ||
@Public() | ||
health(): string { | ||
return 'UP'; | ||
return 'UP' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AdminGuard } from './admin.guard' | ||
|
||
describe('AdminGuard', () => { | ||
it('should be defined', () => { | ||
expect(new AdminGuard()).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common' | ||
import { User } from '@prisma/client' | ||
import { Observable } from 'rxjs' | ||
|
||
@Injectable() | ||
export class AdminGuard implements CanActivate { | ||
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> { | ||
const request = context.switchToHttp().getRequest() | ||
const user: User = request.user | ||
|
||
return user.isAdmin | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import { Controller, Param, Post, Query } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { UserAuthenticatedResponse } from './auth.types'; | ||
import { Controller, Param, Post, Query } from '@nestjs/common' | ||
import { AuthService } from './auth.service' | ||
import { UserAuthenticatedResponse } from './auth.types' | ||
import { Public } from '../decorators/public.decorator' | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor( | ||
private authService: AuthService | ||
) {} | ||
constructor(private authService: AuthService) {} | ||
|
||
@Post('send-otp/:email') | ||
async sendOtp(@Param('email') email: string): Promise<void> { | ||
await this.authService.sendOtp(email); | ||
} | ||
@Public() | ||
@Post('send-otp/:email') | ||
async sendOtp(@Param('email') email: string): Promise<void> { | ||
await this.authService.sendOtp(email) | ||
} | ||
|
||
@Post('validate-otp') | ||
async validateOtp( | ||
@Query('email') email: string, | ||
@Query('otp') otp: string) | ||
: Promise<UserAuthenticatedResponse> { | ||
return await this.authService.validateOtp(email, otp); | ||
} | ||
@Public() | ||
@Post('validate-otp') | ||
async validateOtp(@Query('email') email: string, @Query('otp') otp: string): Promise<UserAuthenticatedResponse> { | ||
return await this.authService.validateOtp(email, otp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AuthGuard } from './auth.guard' | ||
|
||
describe('AuthGuard', () => { | ||
it('should be defined', () => { | ||
expect(new AuthGuard(null, null, null)).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common' | ||
import { JwtService } from '@nestjs/jwt' | ||
import { Request } from 'express' | ||
import { PrimsaRepository } from '../prisma/prisma.repository' | ||
import { Reflector } from '@nestjs/core' | ||
import { IS_PUBLIC_KEY } from '../decorators/public.decorator' | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
private jwtService: JwtService, | ||
private repository: PrimsaRepository, | ||
private reflector: Reflector | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ | ||
context.getHandler(), | ||
context.getClass() | ||
]) | ||
if (isPublic) { | ||
return true | ||
} | ||
|
||
const request = context.switchToHttp().getRequest() | ||
const token = this.extractTokenFromHeader(request) | ||
if (!token) { | ||
throw new UnauthorizedException() | ||
} | ||
try { | ||
const payload = await this.jwtService.verifyAsync(token, { | ||
secret: process.env.JWT_SECRET | ||
}) | ||
request['user'] = await this.repository.findUserById(payload.id) | ||
} catch { | ||
throw new UnauthorizedException() | ||
} | ||
return true | ||
} | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const [type, token] = request.headers.authorization?.split(' ') ?? [] | ||
return type === 'Bearer' ? token : undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.