-
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.
[feat] user api 완성, login guard 전역 설정 (#113)
* feat: redis 값꺼내오기 * feat: login Guard * feat: user api 완성, login guard 전역 설정 * fix: console log 삭제 * fix: 리뷰반영 * fix: 리뷰 반영 * fix: lint 적용 * fix: remove unused modules * fix: login 후 session.userId로 확인 * fix: login Guard global setting 수정 * fix: login Guard 오류 해결 * fix: review 반영 * fix: review 반영
- Loading branch information
Showing
12 changed files
with
108 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
import { Observable } from 'rxjs'; | ||
|
||
interface LoggedInSession { | ||
cookie: any; | ||
userId: string | undefined; | ||
} | ||
|
||
@Injectable() | ||
export class LoggedInGuard implements CanActivate { | ||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
private readonly logger = new Logger(LoggedInGuard.name); | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest() as Request; | ||
const sid = this.getSidFromCookie(request.headers.cookie); | ||
// TODO token이 유효한지 확인하는 로직이 필요함 | ||
return sid !== null; | ||
} | ||
const session = request.session as unknown as LoggedInSession; | ||
const isAuthenticated = session?.userId ? true : false; | ||
|
||
getSidFromCookie(cookieString) { | ||
const cookies = cookieString.split('; ').reduce((acc, cookie) => { | ||
const [key, value] = cookie.split('='); | ||
acc[key] = value; | ||
return acc; | ||
}, {}); | ||
if (isAuthenticated) { | ||
this.logger.debug(`Authenticated: ${session?.userId}`); | ||
} else { | ||
this.logger.debug(`Unauthenticated`); | ||
} | ||
|
||
return cookies['connect.sid']; | ||
return isAuthenticated; | ||
} | ||
} |
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
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