I am trying to use a new Firebase with an Auth system and restrict routes in $routeProviderthrough resolves.
However, I do not quite understand.
This is what I have. In my .config function, I define firebase routes and initialization. My configuration block contains the following.
$routeProvider
.when('/', {
templateUrl: 'templates/dashboard.ejs',
//resolve. this needs restricted
})
.when('/login', {
templateUrl: 'templates/login.ejs'
})
firebase.initializeApp(config);
I found on the new docs site that these functions are available, which are listed in my block .run()for angular.
.run(function($rootScope, $location) {
$rootScope.authentication = firebase.auth();
/*firebase.auth().onAuthStateChanged(function(user) {
if(user) {
$rootScope.user = user;
} else {
$rootScope.user = false;
$location.path('/login')
}
})*/
var user = firebase.auth().currentUser;
if (user) {
$rootScope.user = user;
} else {
$rootScope.user = false;
console.log('No User!');
$location.path('/login');
}
})
Now that I have above, only shooting every other timeI am accessing any URL on my site.
So my question is: how can I take what is in my .run () function and turn it into a solution for my route, so I can limit the routes again.
firebase $firebaseAuth(), , routeChangeError, .
$routeProvider
.when('/', {
templateUrl: 'templates/dashboard.ejs',
resolve: {
"currentAuth": ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase(fbUrl);
var authObj = $firebaseAuth(ref);
return authObj.$requireAuth();
}]
}
})
routechangeerror .run().
.run(function($rootScope, $location) {
$rootScope.$on("$routeChangeError", function(event, current, previous, eventObj) {
if (eventObj === 'AUTH_REQUIRED') {
console.log('auth required!');
$location.path("/login");
}
});
})
, $firebaseAuth(). new Firebase(fbUrl);, .
UPDATE
, , , , .
:
.when('/', {
templateUrl: 'templates/dashboard.ejs',
resolve: {
userAuthenticated: ["$http", "$q", function($http, $q) {
var deferred = $q;
if(firebase.auth().currentUser) {
deferred.resolve();
} else {
deferred.reject();
console.log('Really!?');
}
return deferred.promise;
}]
}
})
routeChangeError.
$rootScope.$on("$routeChangeError", function (event, current, previous, rejection) {
alert("Not authorised");
})
, , , undefined. routeChangeError. console.log() .
, firebase.auth().currentUser null, ?