Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Authentication using Google redirect, redirects to my login page

 firebase.auth().signInWithRedirect(provider);

        firebase.auth().getRedirectResult().then(function (result) {
            if (result.credential) {
                // This gives you a Google Access Token.
                var token = result.credential.accessToken;

            }
            var user = result.user;

        }).catch(function (error) {
            // Handle Errors here.
            console.log(error)
            // ...

        });



   // Start a sign in process for an unauthenticated user.

    firebase.auth().onAuthStateChanged(user => {

        if (user) {

         $state.go('home');

        }
    })

I am trying to have Google Sign-in, but it always redirects me back to my login page and then after a some time it redirects to home page. After I click the sign in button, it redirects to the Google auth and returns back to the login page. How can this be avoided?

like image 220
Ayz Pat Avatar asked Dec 15 '16 07:12

Ayz Pat


1 Answers

You are always calling signInWithRedirect regardless of the auth state. You should call that when you know the user is not signed in. You can do something like this:

    firebase.auth().getRedirectResult().then(function (result) {
        if (!user) {
          // User not logged in, start login.
          firebase.auth().signInWithRedirect(provider);
        } else {
          // user logged in, go to home page.
          $state.go('home');
        }
    }).catch(function (error) {
      // Handle Errors here.
      console.log(error)
      // ...
    });
like image 70
bojeil Avatar answered Oct 05 '22 23:10

bojeil