I'm using Ionic frameworks and ngCordova facebook plugin did a facebook Oauth demo, after success get authenticated from facebook, I've save the token into my localstorage, and redirect the page to profile page, problem comes, after redirect to my profile page, it directly go to else statement, assign $scope.token "no token found!" which is wrong, $localStorage.hasOwnProperty('fbToken') actually is true. If I close the app, and reopen it, it will back to right way, my page will show token, which proved localstorage has the token stored. Can't figure out where is the problem.
Here is my controllers js file
angular.module('starter.controllers', [])
.controller("loginCtrl", ['$scope', '$cordovaFacebook', '$localStorage', '$state', function($scope, $cordovaFacebook, $localStorage, $state) {
$scope.fblogin =function() {
$cordovaFacebook.login(['public_profile', 'email', 'user_friends'])
.then(function(success) {
console.log(success);
$localStorage.fbToken =success.authResponse.accessToken;
$state.go('profile');
}, function (error) {
console.log(error);
});
};
}])
.controller("profileCtrl",['$scope', '$firebase', '$state', '$localStorage', function($scope, $firebase, $state, $localStorage) {
$scope.init =function() {
if($localStorage.hasOwnProperty('fbToken')) {
$scope.token =$localStorage.fbToken;
}else {
$scope.token ="no token found!";
$state.go('login');
}
};
$scope.fblogout =function() {
//clear token
delete $localStorage.fbToken;
//redirect to login page
$state.go('login');
};
}]);