Authentication
The authentication system in this project consists of both client-side and server-side components to handle user authentication. It provides a seamless way to authenticate users, manage user data, and perform authentication-related activities.
Components
auth.tsx and _auth.tsx files handle user authentication logic, with auth.tsx serving the client-side and _auth.tsx the server-side.
Directorysrc/
Directoryutil/
- _auth.tsx
- auth.tsx
Usage
Use the useAuth hook to access user authentication data and user collection data. If you don’t want your user context to contain data from user collection, set combineUserData in auth.tsx to false.
const combineUserData = true;const combineUserData = false;Functions
The authentication system provides several functions to facilitate authentication-related activities. 31SaaS comes pre-built with some commonly used functions. You can build your custom functions on top of these.
| Function | Description |
|---|---|
signup | Register a new user account. |
signin | Sign in an existing user. |
signout | Sign out the current user. |
updateProfile | Update the user’s profile information. |
refetchProfile | Refresh and refetch the user’s profile data. |
updatePassword | Update the user’s password. |
Example usage:
await auth.signout();User Data
You can conveniently access the following user data and calculated data from auth.user when authenticated.
| Variable | Description |
|---|---|
planIsActive | Indicates whether the user’s plan is active. |
isAdmin | Indicates whether the user is an admin. |
planId | Identifier for the user’s current plan. |
planQuantity | Quantity of the user’s current plan. |
Example usage:
const userPlan: string = auth.user.planId;Auth status
The authentication system provides the following status indicators to determine the user’s authentication status. These can be accessed from auth.user.
| Status | Description |
|---|---|
null | In progress of retrieving user’s data |
false | User is not logged in |
| user object | User has logged in |