-
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.
- Intercepter 도입 - 이미 쿠키에 세션이 있었거나, 쿠키에 설정할 세션이 있으면 응답 종류 후에만 업데이트 [#224]
- Loading branch information
Showing
4 changed files
with
64 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { tap } from 'rxjs/operators'; | ||
import { Observable } from 'rxjs'; | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
Injectable, | ||
NestInterceptor, | ||
} from '@nestjs/common'; | ||
import { SessionService } from './session.service'; | ||
import { Response } from 'express'; | ||
|
||
@Injectable() | ||
export class SessionUpdateInterceptor implements NestInterceptor { | ||
constructor(private sessionService: SessionService) {} | ||
|
||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const request = context.switchToHttp().getRequest(); | ||
const response: Response = context.switchToHttp().getResponse(); | ||
let sessionId = request.cookies.sessionId; // 세션 ID 추출 | ||
|
||
return next.handle().pipe( | ||
tap(() => { | ||
sessionId = | ||
sessionId || this.extractSessionId(response.getHeader('Set-Cookie')); // 세션 ID가 없으면 쿠키에서 추출 | ||
// 세션 업데이트 로직 | ||
this.sessionService.saveSession(sessionId); | ||
}), | ||
); | ||
} | ||
|
||
private extractSessionId(cookieStr) { | ||
const sessionIdMatch = /sessionId=([^;]+)/.exec(cookieStr); | ||
return sessionIdMatch ? sessionIdMatch[1] : null; | ||
} | ||
} |
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