how to call getBasicProfile() of google to google signin on only button click?

后端 未结 2 1017
花落未央
花落未央 2021-02-06 15:34

i have take google signin in my website :





        
相关标签:
2条回答
  • 2021-02-06 15:38

    I done it by declaring global js variable as false

    var isFirstGoogle = 0;
    

    Then to check this variable

    if(isFirstGoogle)
    {
    //wont enter here first time
    }
    isFirstGoogle = 1;
    

    So next time when I click on button the above method will be called as now isFirstGoogle = 1;

    Hope this help!! It's a temporary thing I know but it's working for me.

    0 讨论(0)
  • 2021-02-06 15:46

    Best solution is to render sign-in button only when user is not signed in.

    <html>
    <head>
       <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
    </head>
    <body>
      <script>
        function onSignIn(googleUser) {
          var profile = googleUser.getBasicProfile();
          var user_name = profile.getName();
          alert(user_name);
        }
    
        function onLoad() {
          gapi.load('auth2,signin2', function() {
            var auth2 = gapi.auth2.init();
            auth2.then(function() {
              // Current values
              var isSignedIn = auth2.isSignedIn.get();
              var currentUser = auth2.currentUser.get();
    
              if (!isSignedIn) {
                // Rendering g-signin2 button.
                gapi.signin2.render('google-signin-button', {
                  'onsuccess': 'onSignIn'  
                });
              }
            });
          });
        }
      </script>
    
      <div id="google-signin-button"></div>
    
      <script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题