최초 작성일 : 2023-08-16 | 수정일 : 2025-04-13 | 조회수 : 658 |
구글 로그인을 웹사이트에 연동하기 위해 "OAuth 2.0"을 이용하면 됩니다. 아래는 간단한 스텝으로 구글 로그인을 웹사이트에 연동하는 방법입니다:
Google Developer Console에서 프로젝트 생성
클라이언트 ID 및 비밀번호 획득
Google Sign-In 라이브러리를 웹사이트에 포함
html<script src="https://apis.google.com/js/platform.js" async defer>script>
로그인 버튼 추가
html<div class="g-signin2" data-onsuccess="onSignIn">div>
로그인 콜백 처리
onSignIn
함수가 호출됩니다. 이 함수에서 사용자 정보를 가져와 웹사이트에 로그인 처리를 할 수 있습니다:javascriptfunction onSignIn(googleUser)
{
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
// TODO: 서버로 토큰을 전송하여 서버에서의 인증을 진행합니다.
var id_token = googleUser.getAuthResponse().id_token;
}
서버 측 인증 (옵션)
id_token
을 서버로 전송하여 추가적인 인증 및 사용자 세션 관리를 할 수 있습니다.로그아웃 처리 (옵션)
javascriptfunction signOut()
{
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function ()
{
console.log('User signed out.');
});
}
이제 웹사이트에 구글 로그인을 연동했습니다. 사용자들이 구글 계정을 사용하여 웹사이트에 로그인 할 수 있게 되었습니다.