在与 GitHub 之间成功进行重定向身份验证后,我在让 AngularFire 的 $onAuth() 触发时遇到问题。如果我使用auth.$authWithOAuthRedirect('github')
它将重定向到 GitHub 并在成功验证后再次返回,但是当它返回到我的 Angular 应用程序时,auth.$onAuth()
不会调用回调。如果我然后刷新页面,它会被预期的 authinfo 调用。如果我退出 ( $unauth()
) 并重新登录,则在$onAuth()
刷新页面之前不会再次调用回调。
起初我认为这是我做错的事情,但我注意到如果我更改代码以$authWithOAuthPopup()
改用,应用程序会按预期工作:$onAuth()
一旦弹出窗口关闭,就会调用回调,并传递 authdata。
请注意,如果我只是误解了如何使用它,请注意这是否是 AngularFire 中的一个错误。我已经搜索了示例,并且我认为我正确使用了它,但是如果问题出在我的代码中,请告诉我应该如何做。
我已经将一个最小的 repo 案例放入 GitHub repo:https ://github.com/drstearns/angularfire-oath-redir 。当前使用的代码$authWithOAuthRedirect
,你会注意到当你第一次认证时从GitHub返回时,控制台没有任何记录,退出并重新登录后也没有。如果你在登录后刷新页面,它工作正常. 如果您将其更改为使用$authWithOAuthPopup()
,它也可以正常工作。
以下是相关代码的片段:
angular.module('ChatApp', ['firebase'])
.constant('firebaseUrl', 'https://info343chat.firebaseio.com')
.controller('ChatController', function($scope, $firebaseArray, $firebaseObject, $firebaseAuth, firebaseUrl) {
//create reference to the Firebase
var rootRef = new Firebase(firebaseUrl);
//create an authentication service for this firebase
var auth = $firebaseAuth(rootRef);
//when the user clicks the signin button...
$scope.signin = function() {
//authenticate with github using a popup window
//BUG? change this to $authWithOAuthPopup and
//it works correctly
auth.$authWithOAuthRedirect('github')
.catch(function(err) {
console.error(err);
});
};
//when the user clicks the signout button...
$scope.signout = function() {
//un-authenticate (sign out)
auth.$unauth();
};
//when the authentication state changes...
auth.$onAuth(function(authData) {
//for debugging
//not firing after initial redirect back from GitHub
console.log('$onAuth');
console.log(authData);
//if we have authentication data
if (authData) {
//...
}
else {
//...
}
}); //$onAuth()
//...other controller code
}); //ChatController