맞춤 Google 로그인 버튼 빌드

맞춤 설정으로 Google 로그인 버튼을 만들려면 로그인 버튼을 포함하는 요소를 로그인 페이지에 추가하고, 스타일 및 범위 설정으로 signin2.render()를 호출하는 함수를 작성하고, onload=YOUR_RENDER_FUNCTION 쿼리 문자열과 함께 https://apis.google.com/js/platform.js 스크립트를 포함합니다.

다음은 맞춤 스타일 매개변수를 지정하는 Google 로그인 버튼의 예입니다.

다음 HTML, 자바스크립트 및 CSS 코드로 위의 버튼을 생성합니다.

<html>
<head>
  <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
</head>
<body>
  <div id="my-signin2"></div>
  <script>
    function onSuccess(googleUser) {
      console.log('Logged in as: ' + googleUser.getBasicProfile().getName());
    }
    function onFailure(error) {
      console.log(error);
    }
    function renderButton() {
      gapi.signin2.render('my-signin2', {
        'scope': 'profile email',
        'width': 240,
        'height': 50,
        'longtitle': true,
        'theme': 'dark',
        'onsuccess': onSuccess,
        'onfailure': onFailure
      });
    }
  </script>

  <script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>
</body>
</html>

data- 속성을 g-signin2 클래스로 div 요소에 정의하여 맞춤 Google 로그인 버튼의 설정을 지정할 수도 있습니다. 예를 들면 다음과 같습니다.

<div class="g-signin2" data-width="300" data-height="200" data-longtitle="true">

맞춤 그래픽이 있는 버튼 빌드

사이트 디자인에 맞게 Google 로그인 버튼을 만들 수 있습니다. 브랜드 가이드라인을 따르고 버튼에 적절한 색상과 아이콘을 사용해야 합니다. 브랜드 가이드라인에서는 버튼 디자인에 사용할 수 있는 아이콘 애셋도 제공합니다. 또한 버튼이 다른 서드 파티 로그인 옵션만큼 눈에 띄게 해야 합니다.

다음은 맞춤 그래픽으로 빌드된 Google 로그인 버튼의 예입니다.

다음 HTML, 자바스크립트 및 CSS 코드로 위의 버튼을 생성합니다.

<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
  <script src="https://apis.google.com/js/api:client.js"></script>
  <script>
  var googleUser = {};
  var startApp = function() {
    gapi.load('auth2', function(){
      // Retrieve the singleton for the GoogleAuth library and set up the client.
      auth2 = gapi.auth2.init({
        client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',
        cookiepolicy: 'single_host_origin',
        // Request scopes in addition to 'profile' and 'email'
        //scope: 'additional_scope'
      });
      attachSignin(document.getElementById('customBtn'));
    });
  };

  function attachSignin(element) {
    console.log(element.id);
    auth2.attachClickHandler(element, {},
        function(googleUser) {
          document.getElementById('name').innerText = "Signed in: " +
              googleUser.getBasicProfile().getName();
        }, function(error) {
          alert(JSON.stringify(error, undefined, 2));
        });
  }
  </script>
  <style type="text/css">
    #customBtn {
      display: inline-block;
      background: white;
      color: #444;
      width: 190px;
      border-radius: 5px;
      border: thin solid #888;
      box-shadow: 1px 1px 1px grey;
      white-space: nowrap;
    }
    #customBtn:hover {
      cursor: pointer;
    }
    span.label {
      font-family: serif;
      font-weight: normal;
    }
    span.icon {
      background: url('/identity/sign-in/g-normal.png') transparent 5px 50% no-repeat;
      display: inline-block;
      vertical-align: middle;
      width: 42px;
      height: 42px;
    }
    span.buttonText {
      display: inline-block;
      vertical-align: middle;
      padding-left: 42px;
      padding-right: 42px;
      font-size: 14px;
      font-weight: bold;
      /* Use the Roboto font that is loaded in the <head> */
      font-family: 'Roboto', sans-serif;
    }
  </style>
  </head>
  <body>
  <!-- In the callback, you would hide the gSignInWrapper element on a
  successful sign in -->
  <div id="gSignInWrapper">
    <span class="label">Sign in with:</span>
    <div id="customBtn" class="customGPlusSignIn">
      <span class="icon"></span>
      <span class="buttonText">Google</span>
    </div>
  </div>
  <div id="name"></div>
  <script>startApp();</script>
</body>
</html>