Search code examples
angularjsauthenticationangular-ui-routersatellizer

UI Router and Satellizer force login


I know this question isn't very specific, but I have no idea how to solve this issue or where I should begin..

I'm using Satellizer for authentication with UI-Router. the problem is I have 2 routes /sign_in and /profile the /sign_in issue a request to the server returning the user info (when successful login)... this currentUser info is used all over the application...

the problem is when a user try to go to /profile before login, it renders the empty view... how do I enforce user to login first before accessing these "restricted" views ?!

again I know this isn't a specific question but I really don't know where to start


Solution

  • I found the answer in this Github Repo

    the idea is to have role based authentication as discussed in the Repo readme.

    so the code for forbidding the unauthorised access to /profile would be something like the following

      $stateProvider.state("profile", {
        url: "/profile",
        templateUrl: "app/profile/profile.html",
        controller: "ProfileCtrl",
        controllerAs: "user",
        data: {
          permissions: {
            except: ['guest'],
            redirectTo: 'sign_in'
          }
        }
      });
    

    given that you defined the guest role

    definePermissions = function(Permission, Identity) {
      Permission.defineRole('guest', function(stateParams) {
        return !Identity.currentUser;
      });
    };