useMerchant and useRoles are admin hooks — they expose operations that require elevated
permissions on the backend. Build them into admin-only routes protected by AuthGuard with
requiredRoles={['ADMIN']}.
useMerchant
FetchesGET /merchants/me on mount. Exposes methods for updating merchant settings, managing
the theme, and generating API keys. This is the hook behind the MerchantSettings component.
Return Shape
The loaded merchant record.
null while loading on mount. Contains tenant settings
including primaryColor, secondaryColor, loginMode, signupMode, and totpAllowed.true during the initial fetch and while an update call is in progress.Last error message from any operation. Cleared automatically at the start of the next
operation or manually via
clearError().Re-fetches
GET /merchants/me and updates the local merchant state.Updates the merchant record via
PATCH /merchants/:id. Returns the updated merchant.Calling
update() automatically applies the updated primaryColor and secondaryColor
to the current session via applyTheme(). You do not need to call any theme method
manually — the UI updates immediately.Generates a new API key for this merchant. Throws
'No merchant loaded' if called before
mount completes — guard with if (!merchant) return before calling.Revokes an API key by its ID. Throws
'No merchant loaded' if the merchant has not
loaded yet.Manually clear the
error state.MerchantResponse Type
UpdateMerchantRequest Type
Updating Theme Colors
primaryColor and secondaryColor in UpdateMerchantRequest are CSS hex color strings
(e.g., #133e8b). When you call update() with new colors, the library calls applyTheme()
internally — all ThemeProvider CSS variables update immediately without a page reload.
API Key Management
addApiKey(label) calls POST /merchants/:id/api-keys and returns the full API key in
GenerateApiKeyResponse.apiKey. This is the ONLY time the key is available — it is not
returned again after creation.
Protecting Admin Routes
useRoles
useRoles provides full role lifecycle management — list, create, delete, assign, and revoke.
It fetches all roles for the app on mount. This is the hook behind the RolesManager component.
Return Shape
All roles defined for this app — both
SYSTEM (built-in) and MERCHANT (custom) roles.true during the initial roles fetch.Last error message from a fetch operation.
Re-fetches
GET /roles and updates the local roles array.Creates a new MERCHANT-owned role and appends it to the local roles array. Returns the
created role including its generated
id.Deletes a MERCHANT-owned role and removes it from the local array.
Only MERCHANT-owned roles (
role.owner === 'MERCHANT') can be deleted. SYSTEM roles
are built-in and cannot be removed.Assigns a role to a user. Accepts an optional
expiresAt ISO 8601 string for time-limited
role grants. Does NOT update the local roles array — the role list is app-level, not
user-level.Removes a role from a user. Does NOT update the local roles array.
Key Types
Role Hierarchy
Roles have two
owner values:SYSTEM— built-in roles defined by the ailita backend (e.g.,ADMIN,USER). Cannot be deleted.MERCHANT— custom roles created by your tenant. Created viaadd(), deleted viaremove().
priority field determines precedence. Lower priority number = higher authority. Assign
MERCHANT custom roles priorities of 100 or higher to avoid conflicts with SYSTEM roles (which
typically use 1–10).Creating and Assigning a Custom Role
Create the role definition
Call
add() with a name and priority. Use a priority of 100 or higher for custom roles.Get the role ID from the result
The returned
RoleResponse contains the generated id — save it for the assign call.Always read
app and mid from useMerchant().merchant — do not hardcode them. These
values tie the assignment to the correct tenant.Time-Limited Role Grants
AssignRoleRequest.expiresAt accepts an ISO 8601 datetime string for temporary role grants:
Using Roles for Access Control
Next steps
Hooks Reference
All five hooks in one place — useAuth, useUser, useSessions, useMerchant, and useRoles.
Component Reference
Prop tables for ProfileForm, PasswordForm, EmailChangeForm, SessionsList, and TOTPSetup.

