我有一个Firebase AngularJS应用程序,我使用FirebaseSimpleLogin
来执行身份验证,但我在理解如何将登录用户保留在我的身上时遇到了一些麻烦应用。每次登录并刷新页面时,我都会看到我不再进行身份验证。我知道登录后会生成一个令牌,用于对用户进行身份验证,但我是否需要在每个页面上使用此令牌对它们进行身份验证,如果是这样,我会在应用程序和firebase之间产生任何类型的开销?
执行登录/验证的应用程序部分
var ref = new Firebase("https://xxx.firebaseio.com/");
angularFireAuth.initialize(ref, {scope: $scope, name: "user", path:"/login"});
// Used to authenticate user at a later time
$scope.auth = function(token, callback)
{
ref.auth(token, function(error, data) {
if(error)
{
console.log("Login Failed!", error);
}
else
{
$scope.$apply(function()
{
$scope.user = data.auth;
});
}
if (typeof callback === 'function')
{
callback(error,data);
}
});
};
// Login to fetch authentication token to be used by $scope.auth()
$scope.login = function()
{
angularFireAuth.login("password", {
email: $scope.credentials.email,
password: $scope.credentials.password
});
};
// On login stores user within $scope
$scope.$on("angularFireAuth:login", function(evt, user) {
$scope.$apply(function()
{
// Is it possible to save this user accross my application not just in this $scope
$scope.user = user;
});
});
以下是捕获电子邮件/密码组合的标记
<div class="container-single" center>
<h3>Sign in</h3>
<input type="text" ng-model="credentials.email" placeholder="Username" />
<input type="password" ng-model="credentials.password" placeholder="Password" />
<input type="checkbox" id="checkbox-1" name="checkbox-1" /> <label>Remember my password</label>
<button ng-click="login()">Sign in</button>
</div>
答案 0 :(得分:2)
如果您使用angularFireAuth
,则无需自行进行任何身份验证。只需检查$scope.user
值,查看用户是否在每个视图中都已登录。
var ref = new Firebase("https://xxx.firebaseio.com/");
angularFireAuth.initialize(ref, {scope: $scope, name: "user", path:"/login"});
$scope.login = function() {
angularFireAuth.login("password", {
email: $scope.credentials.email,
password: $scope.credentials.password
});
};
成功登录后,$scope.user
会自动设置为用户详细信息。