/api/oauth/google
을 호출 할 경우 AuthGuard로 인해 자동으로 구글 로그인 페이지로 redirect됩니다.@Get()
@UseGuards(AuthGuard("google"))
@ApiOperation({
summary: "구글 로그인 페이지로 리다이렉트",
})
googleOAuth() {
// redirect google login page
return { msg: "Google Authentication" };
}
그 후, 로그인을 완료할 경우 아래의 구글 측에서 /api/oauth/google/callback
으로 access token과 함께 callback합니다.
@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 });
}
/api/oauth/google/callback
API를 호출한적이 없으므로 로그인 여부를 알 수 없고, 유저 정보 또한 얻을 수 없습니다./api/oauth/google/login
API 호출시 POST
를 통해 함께 보낸다.