Initiate Google Login at the click of a button

Hello, I am using google api to login to my web application. Now it works fine. But the problem is that sigin starts automatically when I go to the login page. But I do not want this. I want the user to click the Register button first, and the process will begin.

I use the button below

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

and google log in .

+9
javascript google-plus google-signin
source share
2 answers

The best way is to use Java Script to google login, as I 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. Finally, call the login method

 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); } 

Using this, I was able to solve this problem. Now the user needs to click on the login to enter the system

+1
source share

Use the attachClickHandler method to sign in to Google.

 <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> 
+3
source share

All Articles