Authentication Utilities
This document covers the utility functions for authentication and authorization in the BuildAppsWith platform. These utilities help with common auth-related tasks such as permission checking, token management, and auth state validation.Core Auth Utilities
Role and Permission Utilities
hasRole
Checks if a user has a specific role.| Parameter | Type | Description |
|---|---|---|
roles | UserRole[] | Array of user roles |
role | UserRole | Role to check for |
| Type | Description |
|---|---|
boolean | Whether the user has the role |
hasPermission
Checks if a user has a specific permission based on their roles.| Parameter | Type | Description |
|---|---|---|
roles | UserRole[] | Array of user roles |
permission | Permission | Permission to check for |
| Type | Description |
|---|---|
boolean | Whether the user has the permission |
getRolePermissions
Gets all permissions for a specific role.| Parameter | Type | Description |
|---|---|---|
role | UserRole | Role to get permissions for |
| Type | Description |
|---|---|
Permission[] | Array of permissions for the role |
Token Utilities
parseAuthToken
Parses and validates a JWT auth token.| Parameter | Type | Description |
|---|---|---|
token | string | JWT token to parse |
| Type | Description |
|---|---|
TokenPayload | Decoded token payload |
isTokenExpired
Checks if a token is expired.| Parameter | Type | Description |
|---|---|---|
token | string | JWT token to check |
| Type | Description |
|---|---|
boolean | Whether the token is expired |
Auth State Utilities
isAuthenticated
Checks if a user is authenticated based on auth state.| Parameter | Type | Description |
|---|---|---|
authState | AuthState | Current auth state |
| Type | Description |
|---|---|
boolean | Whether the user is authenticated |
isAuthLoading
Checks if auth state is currently loading.| Parameter | Type | Description |
|---|---|---|
authState | AuthState | Current auth state |
| Type | Description |
|---|---|
boolean | Whether auth state is loading |
Error Handling Utilities
formatAuthError
Formats authentication errors into user-friendly messages.| Parameter | Type | Description |
|---|---|---|
error | unknown | Error from auth operation |
| Type | Description |
|---|---|
string | User-friendly error message |
isAuthError
Type guard to check if an error is an authentication error.| Parameter | Type | Description |
|---|---|---|
error | unknown | Error to check |
| Type | Description |
|---|---|
boolean | Whether the error is an auth error |
Session Utilities
extractUserDataFromSession
Extracts standardized user data from a Clerk session.| Parameter | Type | Description |
|---|---|---|
session | Session | Clerk session object |
| Type | Description |
|---|---|
UserData | Standardized user data |
getRolesFromSession
Extracts user roles from a Clerk session.| Parameter | Type | Description |
|---|---|---|
session | Session | Clerk session object |
| Type | Description |
|---|---|
UserRole[] | Array of user roles |
Implementation Examples
Role-Based Route Protection
Permission Checking
Token Validation
Best Practices
- Always use typed utility functions - Avoid manual role/permission checks
- Handle errors gracefully - Use error helpers to format user-friendly messages
- Validate auth state before using it - Check isAuthenticated/isAuthLoading
- Parse tokens safely - Always use try/catch with token utilities
- Keep utility functions pure - Don’t mix business logic with utility functions