기존 방식의 문제점

oauth (1).png

@Get()
@UseGuards(AuthGuard("google"))
@ApiOperation({
  summary: "구글 로그인 페이지로 리다이렉트",
})
googleOAuth() {
  // redirect google login page
  return { msg: "Google Authentication" };
}
@Post("callback")
@ApiOperation({
  summary: "구글 로그인 callback, 쿠키 생성 후 사용자에게 전송",
})
async googleOAuthCallback(@Body() body: AccessTokenDto, @Res() res: Response) {
  const { accessToken } = body;

  const userInfo = await this.googleOauthService.getUserInfo(accessToken);

  const token = await this.googleOauthService.signIn({
    oauthId: userInfo.data.sub,
    name: userInfo.data.name,
  });

  if (token) {
    res.cookie("access_token", token, {
      sameSite: true,
      secure: false, // 배포시에는 true로 바꿔야됨
      httpOnly: true,
      maxAge: 2 * 60 * 60 * 1000,
    });

    return HttpResponse.success({ validate: true });
  }

  return HttpResponse.success({ validate: false });
}

개선된 방식

Untitled

oauth.png

  1. Front-End에서 OAuth 로그인을 요청한다.
  2. Google Server에서 access token을 발행한다.
  3. Front-End는 발행 받은 access token/api/oauth/google/login API 호출시 POST를 통해 함께 보낸다.
  4. Back-End는 Front-End로 부터 받은 access tokengoogleapis을 이용해 Google Server에 사용자 정보를 요청한다.
  5. 유효한 access token일 경우 Google Server는 해당 사용자의 정보를 반환한다.