Authentication System Overview
better-auth Integration
Proven authentication libraryEmail/password, Social login
Auto Entity Generation
Generate entities via CLIUser, Session, Account, Verification
Context Based
User info per requestSonamu.getContext()
Guards System
Declarative permission control@api guards option
Quick Start
1. Generate Auth Entities
Automatically generate entities required by better-auth:If entities already exist, only missing fields are added. Fields with changed types are
automatically updated.
2. Run Migrations
After generating entities, run migrations:3. Configure Authentication
Enable authentication insonamu.config.ts:
API Endpoints
API endpoints provided by better-auth are automatically registered:Accessing User Info in Context
Access authenticated user information through Context:AuthContext Type
User and Session types are provided by better-auth.
Access Control with Guards
Basic Guard Usage
Implementing guardHandler
Guard logic is implemented inguardHandler in sonamu.config.ts:
In a new project created with
create-sonamu, guardHandler denies by default. user rejects a signed-out request with UnauthorizedException (401); admin rejects a signed-out user or a role other than "admin" with the projectβs ForbiddenException (403, src/exceptions/forbidden-exception.ts); query and custom keys are rejected with 403 until you add their checks. Throwing a plain Error, as in the example above, does not produce a 401 or 403 response.Social Login Setup
Google Login
GitHub Login
Adding User Roles
The default User entity in better-auth doesnβt have arole field. If you need role-based authentication, add it directly to the User entity:
1. Add Field in Sonamu UI
Addrole field to User entity:
- Name:
role - Type:
string - Default:
"user"
"readonly": true on the role prop in entity.json by hand. This removes it from UserBaseSaveParams, so the scaffolded save API cannot change roles. input: false in additionalFields below only blocks auth API requests.
2. Add Enum (Optional)
3. Declare role in additionalFields
Declarerole in user.additionalFields so the session user carries it. input: false stops request bodies of /api/auth/sign-up/email and /api/auth/update-user from setting the value. A role sent at sign-up is ignored and defaultValue is stored instead; a role sent to update-user fails with 400 FIELD_NOT_ALLOWED.
4. Check Role in guardHandler
Client Integration
Using in React
Field Mapping
better-auth uses camelCase, but Sonamu uses snake_case. The following fields are automatically mapped:Checklist
Things to verify after setting up authentication:- Run
pnpm sonamu auth generate - Generate and apply migrations
- Configure
server.authinsonamu.config.ts - Implement
guardHandler - Verify user/session access in Context
- Add role to User entity if role-based auth is needed, and declare it in additionalFields with
input: false
Next Steps
Session Management
Managing sessions and tokens
Context
Learn more about Context
@api Decorator
API decorator options
Error Handling
Handling authentication errors