我正在开发一个在Ionic框架下的应用程序,我也在使用Firebase。
现在,在几个小时后,或者由于应用程序崩溃或重新启动设备,身份验证就会丢失。
怎么能让我的用户总是登录,不管发生什么? (比如Facebook )
下面是来自页面 login.html的登录控制器:
.controller('loginController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {
// Perform the login action when the user submits the login form
$scope.doLogin = function(userLogin) {
if($document[0].getElementById("user_name").value != "" && $document[0].getElementById("user_pass").value != ""){
firebase.auth().signInWithEmailAndPassword(userLogin.username, userLogin.password).then(function() {
var userId = firebase.auth().currentUser.uid;
firebase.database().ref('accounts/' + userId + '/currentBusiness/').update({
name: "No current business arround",
description: "Seems there's nothing arround...",
})
$state.go("tab.favorites");
}, function(error) {
// An error happened.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/invalid-email') {
alert('Enter a valid email.');
return false;
}else if (errorCode === 'auth/wrong-password') {
alert('Incorrect password.');
return false;
}else if (errorCode === 'auth/argument-error') {
alert('Password must be string.');
return false;
}else if (errorCode === 'auth/user-not-found') {
alert('No such user found.');
return false;
}else if (errorCode === 'auth/too-many-requests') {
alert('Too many failed login attempts, please try after sometime.');
return false;
}else if (errorCode === 'auth/network-request-failed') {
alert('Request timed out, please try again.');
return false;
}else {
alert(errorMessage);
return false;
}
});
}else{
alert('Please enter email and password');
return false;
}//end check client username password
};// end $scope.doLogin()
}])发布于 2017-03-28 22:35:42
我会回答我自己的问题,因为我找到了解决办法:
在我的例子中,您必须使用以下代码:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
$state.go("tab.favorites");
// User is signed in.
} else {
// No user is signed in.
}
});发布于 2017-03-28 15:18:32
尝试在登录后在sharedPreferences中或使用外部资源存储登录,然后在用户注销时删除值。
https://stackoverflow.com/questions/43073371
复制相似问题