I have a simple Firebase Facebook OAuth login set like below by following the official tutorial for the Ionic Firebase Facebook login.
The problem is that once I click on the login with Facebook button, I get redirected to Facebook, I log in, and I get redirected back to my app. However, the problem is that I stay in the login page. Strange thing is that if I physically refresh my browser (testing with ionic serve
) by pressing F5 the onAuth
triggers and I get redirected to the Items
page. If I don't refresh the browser, onAuth
doesn't get called.
Did someone have this issue? How did you solve it?
Please note that yes, I did my research (and am slightly starting to lose it), but can't get it to work. I searched SO, tried with $timeout from google groups, tried to call $scope.$apply(), but all to no avail - so please help me figure out where I'm doing it wrong?
.controller('AppCtrl', function($scope, Items, Auth, $state, $ionicHistory) {
$scope.login = function(provider) {
Auth.$authWithOAuthRedirect(provider).then(function(authData) {
}).catch(function(error) {
if (error.code === "TRANSPORT_UNAVAILABLE") {
Auth.$authWithOAuthPopup(provider).then(function(authData) {
});
} else {
console.log(error);
}
});
};
$scope.logout = function() {
Auth.$unauth();
$scope.authData = null;
$window.location.reload(true);
};
Auth.$onAuth(function(authData) {
if (authData === null) {
console.log("Not logged in yet");
$state.go('app.login');
} else {
console.log("Logged in as", authData.uid);
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('app.items');
}
$scope.authData = authData; // This will display the user's name in our view
});
})
<ion-view view-title="Members area">
<ion-content padding="true">
<div ng-if="!authData">
<button class="button button-positive button-block" ng-click="login('facebook')">Log In with Facebook</button>
</div>
<div ng-if="authData.facebook">
<div class="card">
<div class="item item-text-wrap">Hello {{authData.facebook.displayName}}!</div>
</div>
<button class="button button-assertive button-block" ng-click="logout()">Log out</button>
</div>
</ion-content>
</ion-view>
edit: I sort of solved it by using $timeout:
$timeout(function(){
Auth.$onAuth(function(authData) {
if (authData === null) {
console.log("Not logged in yet");
$state.go('app.login');
} else {
console.log("Logged in as", authData.uid);
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('app.items');
}
$scope.authData = authData; // This will display the user's name in our view
});
}, 3000);
However, this just feels wrong (mildly put), and there has to be a better way, so please suggest one. Also, I noticed that the whopping 3second delay is barely enough (few of the resources I found suggested 500ms would be enough, but in my case, that's not the case).
This was a bug that has been fixed in the latest Firebase JS client release (2.3.0). You can now use
$authWithOAuthRedirect
and the $onAuth callback will be triggered when you navigate back to the page.Posted on the github issues, but will reply here as well.
This only happens in the browser using
$authWithOAuthRedirect
. The example was intended for cordova apps, so this really shouldn't be an issue.But if you really need it, you could just skip the redirect and use a popup.