我使用AngularJS和Firebase对我的应用中的用户进行身份验证。登录过程就像一个魅力,但当我刷新我的页面时,它忘记了身份验证状态,我需要再次登录。我如何让它“坚持”?代码如下:
.factory('loginService', ['$firebaseSimpleLogin', '$location', '$rootScope', 'firebaseurl',
function($firebaseSimpleLogin, $location, $rootScope, firebaseurl) {
return {
login: function(email, pass, redirect, callback) {
var dataRef = new Firebase(firebaseurl);
$rootScope.loginObj = $firebaseSimpleLogin(dataRef);
$rootScope.loginObj.$login('password', {
email: email,
password: pass,
rememberMe: true
}).then(function(user) {
console.log('Logged in as: ', user.uid);
}, function(error) {
console.error('Login failed: ', error);
});
},
logout: function(redirectPath) {
$rootScope.loginObj.$logout();
if(redirectPath) {
$location.path(redirectPath);
}
}
}
}])
答案 0 :(得分:0)
我使用angularfire种子https://github.com/firebase/angularFire-seed/添加以下服务:
.run(['loginService', '$rootScope', 'FBURL', function(loginService, $rootScope, FBURL) {
// establish authentication
$rootScope.auth = loginService.init('/admin/signin');
$rootScope.FBURL = FBURL;
}])
让它发挥作用。