Firebase signInSuccessWithAuthResult未调用

时间:2018-08-23 16:49:55

标签: javascript firebase firebase-authentication

我正在为我的网站使用Firebase Auth,但遇到了问题。我想避免登录后自动重定向,据我所知,如果您在回调中返回false,它将不会重定向。我的问题是没有调用signInSuccessWithAuthResult回调。为什么不调用它,并且有更好的方法实现呢?

window.addEventListener('load', function () {
 var uiConfig = {
    callbacks: {
        signInSuccessWithAuthResult: function(authResult, redirectUrl) {
          // User successfully signed in.
          console.log("sign in success");
          // don't redirect automatically
          return false;
        }
    },
    signInSuccessUrl: <I dont want to go here>,
    signInOptions: [
      // Leave the lines as is for the providers you want to offer your users.
      firebase.auth.GoogleAuthProvider.PROVIDER_ID,
      firebase.auth.EmailAuthProvider.PROVIDER_ID
    ],
    // Terms of service url.
    tosUrl: '<your-tos-url>',
    // Privacy policy url.
    privacyPolicyUrl: '<your-privacy-policy-url>'
};

firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
          user.getIdToken().then(function (accessToken) {
              console.log("token retrieved: "+accessToken);
              // Send token to your backend via HTTPS
              var xhr = new XMLHttpRequest();
              xhr.open('POST', '/');
              xhr.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
              xhr.onload = function() {
                console.log('Signed in as: ' + xhr.responseText);
                document.getElementById('firebaseui-auth-container').hidden=true;
              };
              xhr.send('token=' + accessToken);
           });
         }
     });
});

1 个答案:

答案 0 :(得分:0)

在遵循@bojeil技巧后,我更新了库,现在它可以工作了。

请注意,登录后页面仍在重新加载。