-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #355 from bounswe/feature/BE/321/follow-unfollow
added follow and unfollow endpoints for game, added tokenDecoder middleware
- Loading branch information
Showing
9 changed files
with
160 additions
and
51 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
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,23 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { AuthorizedRequest } from '../interfaces/common/authorized-request.interface'; | ||
import { Payload } from '../interfaces/user/payload.interface'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Injectable() | ||
export class TokenDecoderMiddleware implements NestMiddleware { | ||
constructor(private jwtService: JwtService) {} | ||
|
||
async use(req: AuthorizedRequest, res: Response, next: NextFunction) { | ||
const token = this.extractTokenFromHeader(req); | ||
try { | ||
const payload: Payload = await this.jwtService.verifyAsync(token); | ||
req.user = payload; | ||
} catch {} | ||
next(); | ||
} | ||
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
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,35 +1,13 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Request } from 'express'; | ||
import { Payload } from '../../interfaces/user/payload.interface'; | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { AuthorizedRequest } from '../../interfaces/common/authorized-request.interface'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor(private jwtService: JwtService) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request: AuthorizedRequest = context.switchToHttp().getRequest(); | ||
const token = this.extractTokenFromHeader(request); | ||
if (!token) { | ||
throw new UnauthorizedException('access_token_should_be_provided'); | ||
} | ||
try { | ||
const payload: Payload = await this.jwtService.verifyAsync(token); | ||
request.user = payload; | ||
} catch { | ||
throw new UnauthorizedException('token_unauthorized'); | ||
if (!request.user) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const [type, token] = request.headers.authorization?.split(' ') ?? []; | ||
return type === 'Bearer' ? token : undefined; | ||
} | ||
} |