Search code examples
springangularspring-bootjwtclaims

Angular Get Spring Roles from JWT


I have two roles in my REST back-end (built with Spring):

ROLE_USER, ROLE_ADMIN

When I log in, the REST API returns a valid token. Then I set it in the local storage like this:

localStorage.setItem('currentUser', JSON.stringify({username: username, token: token}));

And when I need it I get it like this:

localStorage.getItem('currentUser');

So far so good. When I get my token how do I get the payload and the claims in it?

I want to show admin panel if you are an admin.


Solution

  • jwtHelper.decodeToken($scope.jwt)
    

    See https://github.com/auth0/angular-jwt and https://github.com/auth0/angular2-jwt

    an example https://github.com/auth0-blog/angularjs-jwt-authentication-tutorial/blob/8841ad5d7cb034506d2e3e6840db6202e6969b7a/frontend/home/home.js#L16