Mike
Mike

Reputation: 932

How to handle authorization and authentication on SPA with OAuth2?

I am developing an SPA and would like to have SSO. As I understood so far, OAuth2 with OIDC is the best solution for SPA SSO. Better than, for example, SAML.

What I didn't understand so far is how to use authorization token in SPA's JS code to handle authorization on various resources of SPA. For example, I would like the users with a role 'buyer' to have access to the shopping history tab, where other users won't have access to.

Should I parse access token obtained from Authorization server in JS code and check whether a user has an appropriate role to see the tab, or should this decision be made on server (API) side, in which case SPA's code would just read the answer from API and based on that customize UI?
In case of the first approach, is there any standard way of doing the checking (in form of some JS library)?

When it comes to authentication, what is the better approach (more secure, etc):

Upvotes: 1

Views: 745

Answers (2)

Kavindu Dodanduwa
Kavindu Dodanduwa

Reputation: 13059

According to user description, your application must vary depending on user type. If this is the case I would suggest you to use a backend for authentication and decide application content to be served from the backend. Otherwise, as you have figured out, running authentication on browser and altering user view is not secure.

IMO this not necessarily break SPA architecture. What you are doing is altering what you server based on tokens presented to you. Also, maintaining a session will be required with this approach. And SPA's calls for backend will require to contain this session to obtain contents.

Upvotes: 1

skjagini
skjagini

Reputation: 3217

As soon as the User is logged in, you would request for authentication and based on his UserId, and the role he belongs to you should receive all the permissions that User is entitled to.

You convert these permissions into claims and can send them back to UI and use it appropriately to show the features accordingly.

You also enforce same on the server side api to prevent any unauthorized access besides from your UI.

Upvotes: 0

Related Questions