-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
d4a767b
commit 8bdeb53
Showing
4 changed files
with
102 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,43 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Post, | ||
Request, | ||
UseGuards | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from './auth.guard'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@HttpCode(HttpStatus.OK) | ||
@Post('login') | ||
signIn(@Body() signInDto: Record<string, any>) { | ||
return this.authService.signIn(signInDto.username); | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Post, | ||
Request, | ||
UseGuards | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from './auth.guard'; | ||
import { AuthService } from './auth.service'; | ||
import { ApiProperty, ApiBody, ApiBearerAuth, ApiSecurity } from '@nestjs/swagger'; | ||
class SignInDto { | ||
@ApiProperty({ | ||
example: 'admin', | ||
description: 'Email address of the user', | ||
required: true, | ||
}) | ||
username: string; | ||
} | ||
|
||
@UseGuards(AuthGuard) | ||
@Get('profile') | ||
getProfile(@Request() req) { | ||
return req.user; | ||
} | ||
} | ||
|
||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@ApiBody({ type: SignInDto }) | ||
@HttpCode(HttpStatus.OK) | ||
@Post('login') | ||
signIn(@Body() signInDto: Record<string, any>) { | ||
return this.authService.signIn(signInDto.username); | ||
} | ||
|
||
|
||
@UseGuards(AuthGuard) | ||
@ApiBearerAuth('access-token') | ||
@ApiSecurity('access-token') | ||
@Get('profile') | ||
getProfile(@Request() req) { | ||
return req.user; | ||
} | ||
} |
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,42 +1,44 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { jwtConstants } from './constants'; | ||
import { Request } from 'express'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor(private jwtService: JwtService) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const token = this.extractTokenFromHeader(request); | ||
if (!token) { | ||
throw new UnauthorizedException(); | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { jwtConstants } from './constants'; | ||
import { Request } from 'express'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor(private jwtService: JwtService) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const token = this.extractTokenFromHeader(request); | ||
if (!token) { | ||
throw new UnauthorizedException(); | ||
} | ||
try { | ||
const payload = await this.jwtService.verifyAsync( | ||
token, | ||
{ | ||
secret: jwtConstants.secret | ||
} | ||
); | ||
// 💡 We're assigning the payload to the request object here | ||
// so that we can access it in our route handlers | ||
request['user'] = payload; | ||
} catch { | ||
throw new UnauthorizedException(); | ||
} | ||
return true; | ||
} | ||
try { | ||
const payload = await this.jwtService.verifyAsync( | ||
token, | ||
{ | ||
secret: jwtConstants.secret | ||
} | ||
); | ||
// 💡 We're assigning the payload to the request object here | ||
// so that we can access it in our route handlers | ||
request['user'] = payload; | ||
} catch { | ||
throw new UnauthorizedException(); | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const [type, token] = request.headers.authorization?.split(' ') ?? []; | ||
console.log(request.headers) | ||
console.log(request) | ||
return type === 'Bearer' ? token : undefined; | ||
} | ||
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