[英]Persistent Firebase OAuth Authentication
我正在尝试在多个页面上保持Firebase用户身份验证状态。
$scope.loginGoogle = function() {
console.log("Got into google login");
ref.authWithOAuthPopup("google", function(error, authData) {
$scope.loggedIn = true;
$scope.uniqueid = authData.google.displayName;
}, {
remember: "sessionOnly",
scope: "email"
});
};
function checkLogin() {
ref.onAuth(function(authData) {
if (authData) {
// user authenticated with Firebase
console.log("User ID: " + authData.uid + ", Provider: " + authData.provider);
} else {
console.log("Nope, user is not logged in.");
}
});
};
但是,当在另一个页面中调用checkLogin函数时,即使用户已在登录页面上登录,也未定义authData。 怎么回事?
这里有两件事要知道。
首先,您将JS Client auth方法与AngularFire结合使用。 虽然这不是一件坏事,但您需要了解一些陷阱。
其次,您可以使用AngularFire 0.9中的$firebaseAuth
firebaseAuth模块来处理下面所有的疯狂事情。
使用Firebase JS客户端级别的功能时,Angular不会由于其摘要循环而总是使用它们。 任何外部JS库都是如此。 解决此问题的方法是使用$timeout
服务。
// inject the $timeout service
app.controller("fluttrCtrl", function($scope, $firebase, $timeout) {
var url = "https://crowdfluttr.firebaseio.com/";
var ref = new Firebase(url);
$scope.loginGoogle = function() {
console.log("Got into google login");
ref.authWithOAuthPopup("google", function(error, authData) {
// wrap this in a timeout to allow angular to display it on the next digest loop
$timeout(function() {
$scope.loggedIn = true;
$scope.uniqueid = authData.google.displayName;
});
}, {
remember: "sessionOnly",
scope: "email"
});
});
});
通过将$scope
属性包装在$timeout
将运行另一个循环并将其显示在页面上。
理想情况下,您不想自己处理这个问题。 使用AngularFire内置的$firebaseAuth
firebaseAuth模块。 您需要升级到0.9版本才能使用该模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.