Firebase Auth登录状态持久性不起作用

孟菲斯

在ionic / AngularJS / Cordova IOS应用上。

我正在尝试使用邮件/密码设置持久性登录(后跟Firebase documentationhttps : //firebase.google.com/docs/auth/web/auth-state-persistence)。但是我无法弄清楚我的代码出了什么问题。我的应用程序从登录页面启动,当用户单击“ Connexion按钮”时,它将触发登录功能。

登录正常,但是当我关闭应用程序(不注销)并重新启动它时,我仍在登录页面上。登录名不是永久的。

所以我想,一个问题可能是持久性调用的位置不正确...对吗?根据这篇文章:Firebase 3.0会话持久性可能与有关firebase.auth().onAuthStateChanged(function(user),但我不知道...

// EMAIL CONNEXION TRIGGERED WHEN CONNEXION BUTTON IS HIT

$scope.loginEmail = function($email, $password){
  
  firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(function() {
    var alertPopup;
    
    function signInSuccess(response) {
  
				$state.go("menu.VN");
    }

    function signInError(response) {
    
        var errorCode = null;
        errorCode = response.code;
        if ($email === ""){
             alertPopup = $ionicPopup.alert({
                    title: 'Something wrong...',
                    cssClass: 'pop',
                    template: '<div class="center-form">Need an email address...</div>'
                });   
        }
}

return firebase.auth().signInWithEmailAndPassword($email, $password)
  .then(signInSuccess)
  .catch(signInError);

     })
     .catch(function(error) {
    // Handle persistence Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
  });

};
<label>
          <span>Email</span>
          <input type="email" placeholder="" ng-model="data.email">
</label>

<label>
          <span>Password</span>
          <input type="password" placeholder="" ng-model="data.password">
</label>
    
<button ng-click="loginEmail(data.email, data.password )">Connexion</button>

博耶尔

要保持状态,您应该使用firebase.auth.Auth.Persistence.LOCAL和而不是firebase.auth.Auth.Persistence.SESSION

localStorageCordova iOS应用程序存在易变的已知问题Firebase Auth过去一直依靠,localStorage但最新版本使用更可靠的方式indexedDB来保留用户状态。如果此功能不可用,则会退回到localStorage在这种情况下,您可以将此Microsoft插件用于indexedDB

tldr; 迁移到最新版本的Firebase Auth,并indexedDBindexedDB不可用使用Cordova插件

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章