-
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 #140 from modern-agile-team/feature/board-user
Refactor(2swo/board-user)Board-Gurad생성, 토큰 없을경우 예외처리
- Loading branch information
Showing
5 changed files
with
46 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ExecutionContext, createParamDecorator } from '@nestjs/common'; | ||
|
||
export const BoardOwner = createParamDecorator( | ||
(data, ctx: ExecutionContext): number => { | ||
const req = ctx.switchToHttp().getRequest(); | ||
|
||
return req.unitowner; | ||
}, | ||
); |
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 |
---|---|---|
|
@@ -6,4 +6,4 @@ export const GetUserId = createParamDecorator( | |
|
||
return req.user.userId; | ||
}, | ||
); | ||
); |
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,29 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { TokenService } from 'src/auth/services/token.service'; | ||
import { BoardRepository } from 'src/boards/repository/boards.repository'; | ||
|
||
@Injectable() | ||
export class BoardOwnerGuard { | ||
constructor( | ||
private tokenService: TokenService, | ||
private boardRepository: BoardRepository, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
const request = context.switchToHttp().getRequest(); | ||
const accessToken = request.headers['access_token']; | ||
const boardId = request.query['boardId']; | ||
if (!accessToken) { | ||
request.unitowner = false; | ||
request.user = false; | ||
return true; | ||
} | ||
const userId = await this.tokenService.decodeToken(accessToken); | ||
const board = await this.boardRepository.findBoardById(boardId); | ||
const unitowner = board.userId === userId; | ||
|
||
request.unitowner = unitowner; | ||
request.user = { userId }; | ||
return true; | ||
} | ||
} |