This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## 요약 <br><br> ## 작업 내용 - [x] 탐색탭 api 분리 - [x] 카카오 로그인 닉네임 누락 해결 - [x] 토큰없이 api 접근하기 구현 <br><br> ## 참고 사항 <br><br> ## 관련 이슈 - Close #222 <br><br>
- Loading branch information
Showing
5 changed files
with
94 additions
and
22 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,47 @@ | ||
import Express from 'express'; | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import * as jsonwebtoken from 'jsonwebtoken'; | ||
import { IReqUser } from './user.dto'; | ||
import { UserEntity } from './user.entity'; | ||
|
||
@Injectable() | ||
export class OptionalUserGuard implements CanActivate { | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<Express.Request>(); | ||
const authorization = request.headers['authorization']?.split(' '); | ||
|
||
if ( | ||
authorization && | ||
authorization.length === 2 && | ||
authorization[0] === 'Bearer' | ||
) { | ||
const token = authorization[1]; | ||
|
||
try { | ||
request.user = jsonwebtoken.verify( | ||
token, | ||
process.env.JWT_SECRET, | ||
) as IReqUser; | ||
|
||
// 사용자 검증 | ||
const isValidUser = await UserEntity.findOne({ | ||
where: { | ||
id: request.user.id, | ||
isQuit: false, | ||
}, | ||
}); | ||
|
||
if (!isValidUser) { | ||
return false; | ||
} | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
else{ // 토큰이 없는 경우 | ||
request.user = null; | ||
} | ||
|
||
return true; | ||
} | ||
} |