AngularJS: Check user role to decide if user have permission to view pages or not -


i have problem authentication mechanism. have call api current user roles, check decide user can view pages of app. idea: call api in myapp.run block , check:

angular.module('myapp') .run(['$rootscope', 'authenservice', '$location', function($rootscope, authenservice, $location) {     var currentuser;      function checkrole() {       if(angular.isdefined(currentuser) && angular.isobject(currentuser) && ... && currentuser.isadmin && $location.url() === '/dashboard') {         return true;       }       return false;     }     /*     ** @name: getcurrentuser     ** @description: current user info, use promise $q     */     authenservice.getcurrentuser()      .then(function getsuccess(currentuserinfo){        currentuser = currentuserinfo;        //if user not admin , current page not dashboard page, redirect dashboard page        if(!checkrole()) {          $location.path('/dashboard');        }       }, function geterror(){});      //when user go new path, check role if user have permission access new page or not     $rootscope.$on('$routechangestart', function onroutechange() {        if(!checkrole()) {          $location.path('/dashboard');        }     }) }]; 

my problem: while getcurrentuser api in progress (still not receive response server), code $rootscope.$on('$routechangestart') executed , redirects user dashboard page. should do? have idea/solution resolve problem? or how can wait response server , run userctrl controller(when user go /user, userctrl execute without checkrole because response hasn't been received).

edit

the things want when user goes mysite.com, call api user roles. request still in progress, somehow user goes mysite.com/#/user, user page shows in web immediately(the requirement user can't see user page till request finishes). after request finished, user redirected mysite.com/#/dashboard if user doesn't have permission.

solution

i show loading screen block app till request finished. thank help.

try using $q service:

angular.module('myapp') .run(['$rootscope', 'authenservice', '$location', '$q', function($rootscope, authenservice, $location, $q) {     var currentuser;      function checkrole() {       var defered = $q.defer();       if(angular.isdefined(currentuser) && angular.isobject(currentuser)){          defered.resolve(currentuser.isadmin && ($location.url() === '/dashboard'))        }else{           authenservice.getcurrentuser().then(function getsuccess(currentuserinfo){               currentuser = currentuserinfo;               defered.resolve(currentuser.isadmin && ($location.url() === '/dashboard'))            });       }       return defered.promise;     }      $rootscope.$on('$routechangestart', function onroutechange() {        checkrole().then(function(isadmin){          if(isadmin) $location.path('/dashboard');        })     }) }]; 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -