Authentication Flow
Overview
Authentication Architecture
┌─────────────────────────────────────────────────────────────────┐
│ Client Browser │
└────────────┬────────────────────────────────────────────────────┘
│
│ 1. Initiate Login
▼
┌─────────────────────────────────────────────────────────────────┐
│ SvelteKit App (Edge) │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ Auth Middleware │ │
│ │ • Redirect to FusionAuth │ │
│ │ • Handle callback │ │
│ │ • Resolve identity → user │ │
│ └─────────┬─────────────────────────────────────────────────┘ │
└────────────┼────────────────────────────────────────────────────┘
│
│ 2. OAuth Flow
▼
┌─────────────────────────────────────────────────────────────────┐
│ FusionAuth │
│ • User authentication (email/password, OAuth, SSO) │
│ • Self-registration │
│ • MFA enforcement │
│ • JWT token issuance │
└────────────┬────────────────────────────────────────────────────┘
│
│ 3. JWT returned
▼
┌─────────────────────────────────────────────────────────────────┐
│ SvelteKit App (Edge) │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ Identity Resolution │ │
│ │ • Validate JWT via JWKS │ │
│ │ • Lookup user_identities by fusionauth_id │ │
│ │ • Link identity if needed │ │
│ │ • Store access token in httpOnly cookie │ │
│ │ • Store minimal user info in auth_user cookie │ │
│ └─────────┬─────────────────────────────────────────────────┘ │
└────────────┼────────────────────────────────────────────────────┘
│
│ 4. Database operations with RLS
▼
┌─────────────────────────────────────────────────────────────────┐
│ Neon Postgres │
│ • Validates FusionAuth JWT directly via JWKS │
│ • auth_user() resolves fusionauth_id → internal user_id │
│ • has_permission() checks permissions via auth_permission_cache│
│ • Row-Level Security enforcement │
└─────────────────────────────────────────────────────────────────┘FusionAuth Configuration
FusionAuth Tenant
Applications
ReFresh Platform (User Authentication)
Setting
Value
Login Methods
ReFresh API - Core (API Authorization)
Setting
Value
Role
Description
Scope (OAuth)
Role (Token)
Permissions (DB)
Entity Types (M2M Authentication)
Internal Service Entity Type
Setting
Value
External API Client Entity Type
Setting
Value
Token Type
Tenant Access Determined By
Entities
Entity Name
Type
Purpose
Permissions
Why api-core Needs an Entity
Entity Grants
Caller Entity
Target
Permissions
OAuth Flows
Flow 1: User Login (Browser)
Flow 2: Service-to-Service (Client Credentials)
Flow 3: Third-Party App (User Consent)
Flow 4: Token Refresh
JWT Architecture
FusionAuth JWT → Neon Directly
Property
Value
RLS Helper Functions
Why Direct JWT Validation?
Identity Resolution
User Creation Flow
Pre-Provisioning (Refresh Internal Only)
Resolution Logic
Case-Insensitive Email Matching
HRIS Email
User Logs In As
Match?
Tenant Context
URL Path-Based Tenant Selection
Personal vs Tenant Routes
Route Type
Tenant Context
User ID Used
Example Routes
Token Management
Cookie-Based Token Storage
Cookie
Purpose
httpOnly
Lifetime
Token Refresh Strategy
Why Cookies vs Bearer Tokens?
Approach
Pros
Cons
Security Considerations
JWT Validation
JWKS Endpoint
Self-Registration Security
Troubleshooting
Common Issues
Related Documentation
Last updated