使用Google重定向的Firebase身份验证,重定向到我的登录页面

时间:2016-12-15 07:32:58

标签: angularjs firebase firebase-authentication google-authentication

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

        }
    })

我正在尝试使用Google登录,但它始终会将我重定向回我的登录页面,然后在一段时间后重定向到主页。点击登录按钮后,它会重定向到Google身份验证并返回登录页面。如何避免这种情况?

1 个答案:

答案 0 :(得分:2)

无论auth状态如何,您始终都在调用signInWithRedirect。当您知道用户未登录时,您应该调用它。您可以执行以下操作:

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