Permissions and access control
Learn how use permissions and access control in your supastarter frontend application.
We have already guided you through the process of how implement access control in the API routes of your application. In this guide we will show you how you can protect pages and display UI based on the users role or permissions.
Protect a route (server side)
For authenticated users
To protect a route to be only accessible for authenticated users, you can simply get the session in the RSC component and check if the user is authenticated.
Note: When you are inside the /apps/web/app/(saas)/app
directory, you don't need to check if the user is authenticated, because the session is verified in the middleware.
For specific roles
More interesting is to check if the user has the necessary permissions to access the page. For example, you can make a page only accessible for users with the admin
role.
For active or specifc subscription
Or if you want to check for an active subscription, you can do the following:
For organization role
You can also check if a user has a specific role inside the current organization. For example, you might want to add features that are only available for organization owners or admins.
Display UI based on permissions (client side)
On client side, you can use the useSession
hook to get the session and then check if the user has the necessary permissions.
For authenticated users
Note: You always want to check the permission on the server side first to avoid any security issues.