firebaseAuth GoogleAuthProvider() signInWithRedirect

14,943

Move the getRedirectResult() call out of your loginGoogle() function. getRedirectResult() should be called on page load. An example of this in action can be found here:

https://github.com/firebase/quickstart-js/blob/master/auth/google-redirect.html

Share:
14,943
Yseult Fran
Author by

Yseult Fran

Updated on July 19, 2022

Comments

  • Yseult Fran
    Yseult Fran almost 2 years

    I've a authentication Google with redirect in my app, and I would like just redirect when authentication is completely finished. But the promise is not working

     function loginGoogle() {
            var provider = new firebase.auth.GoogleAuthProvider();
            firebase.auth().signInWithRedirect(provider);
            firebase.auth().getRedirectResult().then(function (result) {
                // This gives you a Google Access Token. You can use it to access the Google API.
                if (result.credential) {
                    var token = result.credential.accessToken;
                    console.log('token ' + token);
                }
                // The signed-in user info.
                var user = result.user;
                console.log('user ' + user);
                // if success redirect to
                $state.go('maps-fullwidth');
    
                // ...
            }).catch(function (error) {
                // Handle Errors here.
                var errorCode = error.code;
                console.log(errorCode);
                var errorMessage = error.message;
                // The email of the user's account used.
                console.log(errorMessage);
                var email = error.email;
                console.log(email);
                // The firebase.auth.AuthCredential type that was used.
                var credential = error.credential;
                console.log(credential);
                // ...
            });
        }
    

    Thanks.

  • Jithin
    Jithin over 5 years
    can we call it like this this.afAuth.auth.signInWithRedirect(provider).then(() => { return this.afAuth.auth .getRedirectResult() .then(result => { console.log(result); const that = this; this.storage .set(TOKEN_KEY, result.user.refreshToken) .then(res => { that.authenticationState.next(true); }); }) .catch(function(error) { alert(error.message); });
  • Jithin
    Jithin over 5 years
    on a then callback in signInWithRedirect
  • D_Edet
    D_Edet almost 4 years
    Thanks a lot Channing Huang. Understand better now.