BolkAuth is split into small packages with a clear boundary between authentication logic and framework/database integrations.
Architecture
Core Engine
@bolkauth/core owns the authentication behavior:
| Area | Responsibility |
|---|---|
| Email/password | Validate credentials, hash passwords, create users and sessions. |
| Magic links | Create verification tokens and verify emailed links. |
| OTP | Generate numeric codes, store hashed tokens, verify one-time use. |
| OAuth | Redirect to providers, validate state, exchange codes, link accounts. |
| Sessions | Sign JWT cookies and persist session records through the adapter. |
| Metadata | Store onboarding and custom user metadata through adapter methods. |
Adapter Boundary
BolkAuth does not own your database client. The adapter is the only database contract the core engine needs:
That boundary lets you use Drizzle, Prisma, MongoDB, or your own storage implementation.
Session Model
After a successful sign-in, BolkAuth:
- Creates a database session through
adapter.createSession. - Signs a JWT containing
sessionIdanduserId. - Sends the JWT in an
HttpOnlycookie. - Uses the cookie on future requests to read the current session.
The cookie is not readable by client-side JavaScript. React hooks call the auth API to load the current user and session.
Request Flow
For protected pages, middleware checks the cookie before rendering and redirects unauthenticated users to signInUrl.
Cross-Tab Sync
BolkAuthProvider uses BroadcastChannel to refresh auth state in every open tab after sign-in, sign-up, and OTP verification. Client components do not need to poll for auth changes.
Errors
API responses use a consistent shape:
Common codes include invalid_credentials, user_exists, weak_password, invalid_email, invalid_otp, otp_not_configured, oauth_error, and unauthorized.