How would you implement role-based access in an Angular app?

Angular routing · Experienced

Model answer

Store the user's roles after login (from the server response or token) in an auth service, ideally with a signal. Use a guard with canActivate or canMatch that checks the required roles in route data and redirects when missing. Hide menu items and buttons the user cannot use.

All of this is for user experience only. Every API request must be authorised on the server, because anyone can change front-end code. Handle expiry and refresh: when the server answers 401, send the user to login.

Tips

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions