javascript - I need both "log in with Google" and "sign up with Google" buttons on the same

Struggling to understand how to achieve this, even though it's extremely monplace across the web.

Struggling to understand how to achieve this, even though it's extremely monplace across the web.

I have two modals, one is a "sign up" modal, the other is a "log in" modal. I need to be able to perform both actions via a user's Google account. I am successfully creating and logging in users via the Google API.

The trouble es with the fact that Google's drop-in button automatically signs the user in.

On the page I have:

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

And later:

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

Obviously these two buttons have different onsuccess functions, but both are being called when the user is logged in. I have somewhat alleviated the problem by only actually getting the Google script on a button click:

$('a#google-login').click(function() {
    $.getScript('.js');
})

But the behaviour of this whole setup is less than ideal. Is there a mon fix for this? It seems incredibly frustrating that Google automatically runs onsuccess functions if the user is logged in (eg without any user action). What's the point of having a button if it runs without user action?

So: I want to be able to log users in via Google, and also sign users up via Google, but only if the user actually clicks a button, in both cases.

Struggling to understand how to achieve this, even though it's extremely monplace across the web.

I have two modals, one is a "sign up" modal, the other is a "log in" modal. I need to be able to perform both actions via a user's Google account. I am successfully creating and logging in users via the Google API.

The trouble es with the fact that Google's drop-in button automatically signs the user in.

On the page I have:

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

And later:

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

Obviously these two buttons have different onsuccess functions, but both are being called when the user is logged in. I have somewhat alleviated the problem by only actually getting the Google script on a button click:

$('a#google-login').click(function() {
    $.getScript('https://apis.google./js/platform.js');
})

But the behaviour of this whole setup is less than ideal. Is there a mon fix for this? It seems incredibly frustrating that Google automatically runs onsuccess functions if the user is logged in (eg without any user action). What's the point of having a button if it runs without user action?

So: I want to be able to log users in via Google, and also sign users up via Google, but only if the user actually clicks a button, in both cases.

Share Improve this question asked Mar 16, 2016 at 10:43 j_dj_d 3,09210 gold badges56 silver badges96 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can achieve what you want by implementing Google Sign-In buttons with imperative approach. My remendation is to use the custom buttons. That way you will get more control over the API. Check out this doc:
https://developers.google./identity/sign-in/web/build-button

And this video might help as well.
https://www.youtube./watch?v=Oy5F9h5JqEU]

Here's a sample code
https://github./GoogleChrome/google-sign-in

As @agektmr said I used the custom button as described in here https://developers.google./identity/sign-in/web/build-button

After that you can create 2 buttons and used a different callback for each one.

This is the code that I had it working with within my react ponent so thats why I'm referencing auth2 from the window object, but you might not need that if your using vanilla JS, This also contains some ES6 so sorry if your not using ES6, you can just convert () => {} to function () { } and let to var

// This method is from the above cited article, I have just altered it slightly so 
// that you can input a success  callback as a parameter rather than hard coding just one
function attachElementToCallback(element, success) {         
  window.auth2.attachClickHandler(element, {}, success    
    , (error) => {
      var message = JSON.stringify(error, undefined, 2);
      alert(message)
    });
}

function initializeGoogleStuff() {
  window.gapi.load('auth2', () => {
    window.auth2 = window.gapi.auth2.init({
      prompt: "select_account",
    })

    let signInElement = window.document.getElementById("signInButton")
    if (signInElement) {
      attachElementToCallback(signInElement, handleLogin)
    }

    let signUpElement = window.document.getElementById("signUpButton")
    if (signUpElement) {
      attachElementToCallback(signUpElement, (response) => console.log(response))
    }

  })
}

for the html (this is just copied and pasted from the above article and duplicated, they have the CSS and further instructions if you want to make it ply with googles branding guidelines (which is a must if you want them to verify your app) https://developers.google./identity/branding-guidelines

<div id="signInButton" class="customGPlusSignIn">
  <span class="icon"></span>
  <span class="buttonText">Sign In</span>
</div>
<div id="signUpButton" class="customGPlusSignIn">
  <span class="icon"></span>
  <span class="buttonText">Sign Up</span>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744674538a4587248.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信