Skip to content

Commit

Permalink
Merge pull request #293 from Quickchive/feat/#292-redirect-google
Browse files Browse the repository at this point in the history
feat: redirect google login
  • Loading branch information
stae1102 authored Mar 3, 2024
2 parents 75eeb39 + 22e3a4b commit b619ceb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/auth/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,17 @@ export class OAuthController {
@UseGuards(AuthGuard('google'))
async googleAuthRedirect(
@AuthUser() user: googleUserInfo,
): Promise<LoginOutput> {
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!);
}
}
18 changes: 18 additions & 0 deletions src/auth/oauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit b619ceb

Please sign in to comment.