angularjs - Firebase (angular) facebook authWithRedirect not working correcting -
i have login facebook button using angularfire (ionic app) , using $authwithoauthredirect redirect facebook authentication page app.
it works if have $onauth method inside of function calls facebook redirect authentication, if it's outside of function doesn't work. have form authenticating email , password , if $onauth inside facebook authentication method email , password login won't work correctly.
here's controller that's using auth service initializing firebaseauth:
/**** * login page controller * uses angularfires oauth facebook login ****/ angular.module('starter') //inject auth service loginservice.js .controller('logincontroller', ['$scope', '$state', 'auth', function($scope, $state, auth) { //userlogin scope variables: error, authdata $scope.userlogin = { error: null }; //login 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 incorrect"; } else { $scope.userlogin.error = "enter valid email , password"; } } ) }; //login function facebook login $scope.loginuserfacebook = function() { auth.$authwithoauthredirect('facebook') .then(function(authdata) { console.log('hi'); }) //if error due no redirects .catch(function(error) { //authenticate popup 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 ' + authdata.uid); } //set authdata on controller scope $scope.userlogin.authdata = authdata; }); }]);
so if move auth.$onauth loginuserfacebook function works, messes loginuser email , password.
i'm guessing facebook redirect doesn't resolve promise thought, i've went through firebase docs extensively , can't figure out.
disclaimer: work @ firebase
the promise returned auth.$authwithoauthredirect(...)
little misleading, because promise never resolved - can fail. in cases you're attempting use browser redirects on platform doesn't support them (phonegap, example) we'll throw transport_unavailable
error , promise rejected. in other "successful" cases, browser redirect oauth provider , application, prior state of promise no longer available.
in order pick authentication session after successful browser redirect, use auth.$onauth(function(authdata){...})
monitor authentication state. on first return oauth provider, firebase client under hood complete creation of session , raise event callback passed onauth(function(authdata){...})
current auth state of user.
read more firebase auth methods here , angularfire's auth tools here.
Comments
Post a Comment