I have created a firebase Auth factory that looks like this:
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;
}
]);
The issue I am facing is that when I inject the Auth factory into my ui-router resolve as a dependency, it ends up being empty because the platform ready function runs after the ui-router configuration.
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
});
}
}
})
I have tried resolving this by altering my factory like so -
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);
});
}
]);
However, this approach returns a promise which complicates the usage even further.
EDIT : To address this issue, I incorporated promises in the factory
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;
}
};
}
]);
Though this solution works, I am still on the lookout for an improved one.