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



On an ionic/AngularJS/Cordova IOS app.

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

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

所以我想,一个问题可能是持久性调用没有很好地放置......是吗?根据这篇文章: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

科尔多瓦 iOS 应用程序中的localStorage存在易失性的已知问题。 Firebase 身份验证过去依赖于localStorage但最新版本使用更可靠的indexedDB来保存用户状态。当这不可用时,它会回退到localStorage。在这种情况下,您可以将此Microsoft插件用于索引数据库。

tldr;迁移到最新版本的 Firebase Auth,并在 CordovaindexedDB插件不可用时使用indexedDB

最新更新