Search code examples
javascriptangularjsspring-mvcauthorizationuser-roles

Authorization with AngularJS


We have a single page application with AngularJS (1.6) and a server side running JAVA Spring MVC. After authenticating, the server delivers the index.htm file with a main JS file containing ALL the application's modules.

Now there is a need to make some modules available to certain user roles. What is the accepted way to achieve this?

I was thinking about creating a filter on the server side to catch requests to that JS file and according to the user role, strip down from the JS file the modules which the user shouldn't see.

It seems like not really an elegant solution and I wonder if there is any better way to achieve this.

Thank you!!


Solution

  • You can see the following example Login example. It uses rest http authentication and uses user name and password to in the headers to generate a encrypted header. See Rest Api for the back end code. See authentication service

      if (LocalStorage.Get('auth') !== null) {
        $http.defaults.headers.common.Authorization = LocalStorage.Get('auth');
        return true;
      }
      else {
        return false;
      }