I have a firebase Auth factory as follows
app.factory("Auth", ["$firebaseAuth", "FIREBASE_URL","$ionicPlatform",
function($firebaseAuth, FIREBASE_URL, $ionicPlatform) {
var auth = {};
$ionicPlatform.ready(function(){
var ref = new Firebase(FIREBASE_URL);
auth = $firebaseAuth(ref);
});
return auth;
}
]);
I am injecting the Auth factory in my ui-router resolve as dependency but by the time ui-router is configured the auth is empty because the platform ready kicks in afterwards.
app.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('menu', {
url: '/menu',
abstract:true,
cache: false,
controller: 'MenuCtrl',
templateUrl: 'templates/menu.html',
resolve: {
auth: function($state, Auth){
//<--Auth is empty here when the app starts ----->
return Auth.$requireAuth().catch(function(){
$state.go('login'); //if not authenticated send back to login
});
}
}
})
How do i make sure the Auth factory is not empty before it is injected into ui-router?
I tried this -
app.factory("Auth", ["$firebaseAuth", "FIREBASE_URL","$ionicPlatform",
function($firebaseAuth, FIREBASE_URL, $ionicPlatform) {
return $ionicPlatform.ready(function(){
var ref = new Firebase(FIREBASE_URL);
return $firebaseAuth(ref);
});
}
]);
But this returns a promise which makes it even more complicated to use.
EDIT : I used promises in factory to fix the problem
app.factory("Auth", ["$firebaseAuth", "FIREBASE_URL","$ionicPlatform","$q",
function($firebaseAuth, FIREBASE_URL, $ionicPlatform, $q) {
var auth = {};
return {
getAuth : function(){
var d = $q.defer();
$ionicPlatform.ready().then(function(){
var ref = new Firebase(FIREBASE_URL);
auth = $firebaseAuth(ref);
d.resolve(auth);
});
return d.promise;
}
};
}
]);
This works but I am looking for a better solution.