Authentication System
The authentication system is configured insrc/lib/server/auth.ts:9 with the following features:
- Email and password authentication - Users sign in with email and password credentials
- Session management - Sessions last 7 days with daily updates (
src/lib/server/auth.ts:37-39) - SQLite database - User data stored via Drizzle ORM adapter
- Organization plugin - Multi-tenant profile support through better-auth’s organization plugin
Database Tables
The authentication system uses these core tables (defined insrc/lib/server/db/schema.ts):
| Table | Purpose | Key Fields |
|---|---|---|
user | User accounts | id, name, email, role, emailVerified |
session | Active user sessions | id, token, userId, expiresAt, activeOrganizationId |
account | OAuth/credential data | id, userId, providerId, password |
verification | Email verification tokens | id, identifier, value, expiresAt |
User Roles
Plank supports two user roles defined in theuser table (src/lib/server/db/schema.ts:14-16):
- admin - Full system access, can create profiles (organizations)
- user - Standard user with profile membership access
First User Admin Promotion
The first user to register automatically becomes an admin (src/lib/server/auth.ts:43-51):
Account Creation
Provide account details
Enter the following information:
- Name - Full name for display
- Email - Valid email address (must be unique)
- Password - Minimum 8 characters
- Confirm Password - Must match password field
src/routes/(auth)/register/+page.svelte:26-30).Submit registration
Click “Create Account” to submit the form. The system:
- Creates a new user account
- Assigns the
adminrole if this is the first user, otherwiseuser - Automatically signs you in
- Redirects to
/profilesor the original requested page
Signing In
Session Management
Sessions are configured with these settings (src/lib/server/auth.ts:36-39):
- Expiration: 7 days (604,800 seconds)
- Update age: 1 day - sessions are refreshed daily during active use
- Active organization: Sessions track the currently selected profile via
activeOrganizationId
Session Data
Each session stores (src/lib/server/db/schema.ts:42-67):
- Session token (unique)
- User ID (foreign key to user table)
- Expiration timestamp
- IP address and user agent for security tracking
- Active organization ID (current profile context)
Security Features
Password Requirements
- Minimum 8 characters enforced client-side and server-side
- Passwords are hashed before storage (handled by better-auth)
Environment Variables
Authentication requires these environment variables:BETTER_AUTH_SECRET- Secret key for signing tokens and sessionsBETTER_AUTH_URL- Base URL for authentication callbacks
Protected Routes
Routes check authentication vialocals.user in server load functions:
/login with a return URL parameter.
API Endpoint
The better-auth API is mounted at/api/auth/[...all] (src/routes/api/auth/[...all]/+server.ts) and handles:
- Sign up requests
- Sign in requests
- Session validation
- Organization operations
- Invitation acceptance
$lib/auth-client) communicates with this endpoint for all authentication operations.