I am utilizing the $routeChangeStart function to redirect authorized users to specific URLs and prevent unauthorized access to special pages.
In addition, I have dynamically generated pages that can be accessed via their unique page slugs. To achieve this, I am using $routeChangeStart to call an $http service that retrieves all page slugs from the database and allows users to navigate to them.
The issue arises when I add a new page from the backend and attempt to view it. The page slug for the newly inserted record is not retrieved from the database, causing the default route to lead me back to the homepage.
However, upon refreshing the application (pressing F5), the correct route is found.
Below is a snippet of my code:
Route configuration for single page :
$routeProvider.when('/:pageSlug',{ templateUrl:BASE_URL+'singlePage', controller:"pageController"});
$routeChangeStart implementation :
app.run(function($rootScope, $location, loginService, PageUrlService, $route)
{
var $promise = PageUrlService.getPageUrls();
$rootScope.$on('$routeChangeStart', function(){
var pageRoutes = [],
after = [],
before = [],
right = [],
general = [];
$promise.then(function(res){
var data = res.data;
if(data.status == "found"){
angular.forEach(data.content, function(value, key){
pageRoutes.push("/"+ value.pageUrl);
});
}
if($rootScope.isLoggedIn == true)
{
// Route access allowed if found in "pageRoutes"
}
else
{
// Route access allowed if found in "pageRoutes"
}
});
});
});