【发布时间】:2015-07-23 16:44:48
【问题描述】:
我使用 angularfire(Ionic 应用程序)通过 facebook 按钮登录,并且正在使用 $authWithOAuthRedirect 重定向到 facebook 身份验证页面,然后返回到应用程序。
如果我在调用 facebook 重定向身份验证的函数内部有一个 $onAuth 方法,它可以工作,但如果它在该函数之外,它就不起作用。我还有一个使用电子邮件和密码进行身份验证的表单,如果 $onAuth 在 facebook 身份验证方法中,那么电子邮件和密码登录将无法正常工作。
这是使用初始化 firebaseAuth 的 Auth 服务的控制器:
/****
* Login Page Controller
* Uses angularfires OAuth for facebook login
****/
angular.module('starter')
//inject the Auth service from loginService.js
.controller('LoginController', ['$scope', '$state', 'Auth', function($scope, $state, Auth) {
//userlogin scope variables: error, authData
$scope.userLogin = {
error: null
};
//Login for email password
$scope.loginUser = function() {
Auth.$authWithPassword({
email: $scope.userLogin.email,
password: $scope.userLogin.password
})
.then(
function(authData){
$scope.userLogin.authData = authData;
},
function(error) {
if(error.code == 'INVALID_EMAIL') {
$scope.userLogin.error = "Invalid Email";
}
else if(error.code == 'INVALID_PASSWORD'){
$scope.userLogin.error = "Email or Password is incorrect";
} else {
$scope.userLogin.error = "Enter a valid email and password";
}
}
)
};
//login function for facebook login
$scope.loginUserFacebook = function() {
Auth.$authWithOAuthRedirect('facebook')
.then(function(authData) {
console.log('hi');
})
//if error due to no redirects
.catch(function(error) {
//authenticate with popup for emulators
if (error.code === 'TRANSPORT_UNAVAILABLE') {
Auth.$authWithOAuthPopup('facebook').then(function(authData) {
// console.log(authData);
});
} else {
console.log(error);
}
});
};
//After successful auth
Auth.$onAuth(function(authData) {
if (authData === null) {
console.log('Not logged in');
}
else {
$state.go('driver');
console.log('Logged in as ' + authData.uid);
}
//set authData on controller scope
$scope.userLogin.authData = authData;
});
}]);
因此,如果我将 Auth.$onAuth 上移到 loginUserFacebook 函数中,那么它可以工作,但会将 loginUser 的电子邮件和密码弄乱。
我猜 facebook 重定向并没有像我想的那样解决承诺,但我已经广泛阅读了 Firebase 文档,但无法弄清楚。
【问题讨论】:
-
如果下面的答案没有为您澄清/解决,请随时在 firebase dot com 上 ping 我。
标签: angularjs firebase ionic facebook-authentication