我有一个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>
如果您使用的是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
将自动设置为用户详细信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句