Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login using Google authentication without opening new window/tab

I want to implement Google login in my site like is done in Stack Exchange, Stack Overflow and more. Meaning that when the user choose Login using Google authentication, the site redirects to a page where he can choose which Google account to use and without opening a new window/tab and redirect to the site Can you help me with links/documentation how to do it Very important not to open new tab/window.

I'm using this: https://developers.google.com/identity/sign-in/web/build-button

You can see that when you click on the button a new window is open, I wand to prevent this.

Thanks in advance

like image 469
Boba Fett likes JS Avatar asked Jun 15 '16 14:06

Boba Fett likes JS


2 Answers

You could try to switch to the JavaScript client authentication and pass the ux_mode option to the signIn() function options.

ux_mode: "redirect"

Documentation here: https://developers.google.com/identity/sign-in/web/reference#googleauthsigninoptions

Then you can get the user profile like so:

let googleUser = window.gapi.auth2.getAuthInstance().currentUser.get();

if (googleUser) {
    let profile = googleUser.getBasicProfile();

    if (profile) {
        this.state.google.firstname = profile.getGivenName();
        this.state.google.lastname = profile.getFamilyName();
        this.state.google.email = profile.getEmail();
    }
}

This is all documented here and there: https://developers.google.com/identity/sign-in/web/people

like image 60
Maxime Asselin Avatar answered Nov 18 '22 18:11

Maxime Asselin


Just add data-ux_mode="redirect" to the sign-in div from the Google's example (or to the .render call if you're rendering the login-button programmatically)

<div class="g-signin2" data-onsuccess="onSignIn" data-ux_mode="redirect"></div>
like image 2
Alex from Jitbit Avatar answered Nov 18 '22 19:11

Alex from Jitbit