From 22e3a4bef00ea8b1f2af7e5a6577cac6b05c990c Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 3 Mar 2024 17:19:58 +0900 Subject: [PATCH] feat: redirect google login --- src/auth/oauth.controller.ts | 14 ++++++++++++-- src/auth/oauth.service.ts | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/auth/oauth.controller.ts b/src/auth/oauth.controller.ts index c76b8ef..36bea3b 100644 --- a/src/auth/oauth.controller.ts +++ b/src/auth/oauth.controller.ts @@ -88,7 +88,17 @@ export class OAuthController { @UseGuards(AuthGuard('google')) async googleAuthRedirect( @AuthUser() user: googleUserInfo, - ): Promise { - return this.oauthService.googleOauth(user); + @Res() response: Response, + ) { + const { access_token, refresh_token } = await this.oauthService.googleOauth( + user, + ); + + const cookieOption = this.oauthService.getCookieOption(); + + response.cookie('accessToken', access_token, cookieOption); + response.cookie('refreshToken', refresh_token, cookieOption); + + response.redirect(process.env.FRONTEND_URL!); } } diff --git a/src/auth/oauth.service.ts b/src/auth/oauth.service.ts index 8f759a6..7b46446 100644 --- a/src/auth/oauth.service.ts +++ b/src/auth/oauth.service.ts @@ -18,6 +18,7 @@ import { User } from '../users/entities/user.entity'; import { UserRepository } from '../users/repository/user.repository'; import * as CryptoJS from 'crypto-js'; import { Cache } from 'cache-manager'; +import { CookieOptions } from 'express'; @Injectable() export class OAuthService { @@ -149,4 +150,21 @@ export class OAuthService { private encodePasswordFromEmail(email: string, key?: string): string { return CryptoJS.SHA256(email + key).toString(); } + + public getCookieOption() { + const cookieOption: CookieOptions = + process.env.NODE_ENV === 'prod' + ? { + httpOnly: true, + sameSite: 'none', + secure: true, + } + : { + httpOnly: true, + sameSite: 'lax', + secure: false, + }; + + return cookieOption; + } }