Authorization
This guide will help you get started with authorization in your SaaS app.
Authorization refers to what users can access in your app. This is useful for differentiating between users who have paid for different subscription tiers (e.g. “hobby” vs “pro”), or between users who have admin privileges and those who do not.
Authorization differs from authentication in that authentication refers to the process of verifying that a user is who they say they are (e.g. logging in with a username and password).
To learn more about the different types of user permissions built into this SaaS template, including Stripe subscription tiers and statuses, check out the User Overview Reference.
Also, check out our blog post to learn more about authorization (access control) in web apps.
Client-side Authorization
Open Saas starts with all users having access to the landing page (/
), but only authenticated users having access to the rest of the app (e.g. to the /demo-app
, or to the /account
).
To control which pages require users to be authenticated to access them, you can set the authRequired
property of the corresponding page
definition in your main.wasp
file:
This will automatically redirect users to the login page if they are not logged in while trying to access that page.
If you want more fine-grained control over what users can access, there are two Wasp-specific options:
- When you define the
authRequired: true
property on thepage
definition, Wasp automatically passes the User object to the page component. Here you can check for certain user properties before authorizing access:
- Or you can take advantage of the
useAuth
hook and check for certain user properties before authorizing access to certain pages or components:
Server-side Authorization
Authorization on the server-side is the core of your access control logic, and determines what users actually can or can’t do (unlike client-side authorization logic which is there merely for UX).
You can authorize access to server-side operations by adding a check for a logged-in user on the context.user
object which is passed to all operations in Wasp: