Teodor Dimitrov
Teodor Dimitrov

Reputation: 1023

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.

Upvotes: 3

Views: 4135

Answers (3)

Kloranthy
Kloranthy

Reputation: 1

The other answers covered the spring part of your question, so I'm going to focus my answer on the angular part.

You can use a directive to show/hide the admin panel like so:

<admin-component *ngIf="user.isAdmin"></admin-component>

Upvotes: 0

Avinash Kumar
Avinash Kumar

Reputation: 1

It is very difficult to identify user role based on the token, Better add a new flag like IS_ROLE_USER, IS_ROLE_ADMIN and based on that flag check the current user role. Or else use spring boot security. Might it will help you.

Upvotes: 0

Related Questions