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; + } }