Answer the question
In order to leave comments, you need to log in
How to make access only for administrators?
I want to make it so that authorized users with admin access can follow admin links. Links for authorized users work, but not for admins. At the same time, if the user is not authorized and follows the admin link, he still sees the page.
PS: I do not understand how to do it, I have been fighting for 5-6 hours. All methods are working ... Thank you in advance!
app.run...
App.run(['$rootScope', '$location', 'User', function($rootScope, $location, User){
$rootScope.$on('$routeChangeStart', function(event, next, current){
if(next.$$route.auth){
if(!User.AuthCheck()){
$location.path('/');
}
if(next.$$route.originalPath == '/'){
if(User.AuthCheck()){
$location.path(current.$$route.originalPath);
}
}
}
if(next.$$route.admin){
if(!User.AuthCheck()){
$location.path('/');
}
if(User.checkAdmin()){
$location.path(current.$$route.originalPath);
}
}
});
$rootScope.AuthCheck = function(){
return User.AuthCheck();
}
$rootScope.checkAdmin = function(){
return User.checkAdmin();
}
}]);
// Освновные роуты
$routeProvide
.when('/', {
templateUrl: 'view/home.html',
controller: 'HomeController',
});
// Для авторизованных
$routeProvide
.when('/auth/logout', {
templateUrl: 'view/auth/logout.html',
controller: 'UserController',
auth: true
});
$routeProvide
.when('/admin', {
templateUrl: 'view/admin/home.html',
controller: 'AdminController',
auth: true,
admin: true
});
$routeProvide.otherwise('/');
Answer the question
In order to leave comments, you need to log in
in the page controller for administrators, you can check whether it is authorized or not, if not, send it somewhere.
I use ui-router, I usually do this:
angular.module('app.config').run(run);
function run($rootScope, $state, $auth, $stateParams) {
$rootScope.$on('$stateChangeStart', function (event, toState, toStateParams) {
if (!$auth.isAuthenticated() && toState.name !== 'signin') {
$rootScope.toState = toState;
$rootScope.toStateParams = toStateParams;
event.preventDefault();
$state.go('signin', {});
}
});
}
I am using ui-router along with the angular-permission module .
You can create different roles and redirect the user based on them.
I solved the problem myself, in my own way) Thanks to everyone who helped!
Here is the code of my solution, suddenly it will come in handy)
$rootScope.$on('$routeChangeStart', function(event, next, current){
if(next.$$route.auth){
if(!User.AuthCheck()){
$location.path('/');
}
if(next.$$route.originalPath == '/'){
if(User.AuthCheck()){
$location.path(current.$$route.originalPath);
}
}
}
if(next.$$route.admin){
if(!User.checkAdmin()){
$location.path('/');
}
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question