13

Hello I am using google api to sign in to my web application . Now Its working fine . But the problem is the sigin is initiated automatically as I go to my log in page . But I do not want that . I want the user to click the signin button first then the process will start .

I am using below button

<div class="g-signin2" data-onsuccess="onSignIn"></div>

and google log in references .

Abdullah Al Noman
  • 2,817
  • 4
  • 20
  • 35
  • Have you added the `Sign Out Button`. I tried using the `google-signin` a few months back and it worked fine. I'm guessing you're logged in to you google account as you're trying this. If that's the case you should logout first and then try it. – thegauravmahawar Sep 17 '15 at 06:48
  • As I said it works fine on my current flow . But I do not want user to log in automatically if they were logged in before . I want to start the flow when The user clicks the button .If in any case when he is redirected to log in page logged in or out he will have to click the sign in button again :) . I am using a spring security at back end so My requirement is somewhat odd but it is what it is :( . @thegauravmahawar – Abdullah Al Noman Sep 17 '15 at 06:55
  • 1
    It's just a "maybe" but what if you could call the `sign out function` when; as you said - "..I want to start the flow when the user clicks the button". So is it possible that when the user clicks the button you can call the `sign out function` first. I have never tried it so it's just a guess. Let me know anyways :) – thegauravmahawar Sep 17 '15 at 07:02
  • That is a good idea actually I will log out user if he is logged in @thegauravmahawar let me try – Abdullah Al Noman Sep 17 '15 at 08:55
  • I have tried to log out . but it seems there is no error but the user does not get logged out @thegauravmahawar – Abdullah Al Noman Sep 23 '15 at 05:09

2 Answers2

9

Use 'attachClickHandler' to Initiate Google Sign In.

<script src="https://apis.google.com/js/client:platform.js?onload=init" async defer></script>
<script>
   function init() {
        gapi.load('auth2', function() {
            auth2 = gapi.auth2.init({
                client_id: '*************.apps.googleusercontent.com',
                cookiepolicy: 'single_host_origin',
                scope: 'profile email'
            });
            element = document.getElementById('glogin');
            auth2.attachClickHandler(element, {}, onSignUp, onFailure);
        });
    }
    function onSignUp(googleUser) {
      var profile = googleUser.getBasicProfile();
    }
</script>
parth kabariya
  • 246
  • 2
  • 6
  • Thanks for sharing! Here's the official documentation on `attachClickHandler` for others that may be curious: https://developers.google.com/identity/sign-in/web/reference#googleauthattachclickhandlercontainer_options_onsuccess_onfailure – Alexander Mar 26 '20 at 23:21
  • 2
    can you complete the example by adding the code for creating the google button for glogin – ealeon Oct 12 '20 at 07:58
  • Warning: The Google Sign-In JavaScript platform library for Web is set to be deprecated after March 31, 2023 – Zain Qasmi Feb 01 '23 at 17:40
1

Best way is to use Java Script to sign in to google as I have found out .

use

script type="text/javascript">
      (function() {
       var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
       po.src = 'https://apis.google.com/js/client.js?onload=onLoadCallback';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
     })();
</script>

to initialize js . And finally call below method to sign-in user

function login() 
{
  var myParams = {
    'clientid' : 'YOUR_CLIENT_ID.apps.googleusercontent.com', //You need to set client id
    'cookiepolicy' : 'single_host_origin',
    'callback' : 'loginCallback', //callback function
    'approvalprompt':'force',
    'scope' : 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read'
  };
  gapi.auth.signIn(myParams);
}

By using this I was able to solve this problem . Now User needs to click on sign-in to login

Abdullah Al Noman
  • 2,817
  • 4
  • 20
  • 35
  • can you please tell me how can i get google data through this process ?? i am using callback function to get data but i am getting this error "Callback function named "onGoogleLoginSuccess" not found". – Nayyar Jamal Jan 27 '17 at 07:50
  • @NayyarJamal loginCallback give your method name there the method will be called with all the values it means your function is wrong or your function is not reachable from this page – Abdullah Al Noman Feb 27 '17 at 08:09
  • HI abdullah can you share full code for google login? – nick Jun 22 '17 at 06:11
  • @hey sorry I forget the password for that Id :/ now I am using this one – Abdullah Al Noman Aug 18 '17 at 12:36