Back to Specifications

Magic Link Authentication

Passwordless authentication flow using email magic links with time-limited tokens and security validation.

Complete Flow Diagram

100%

Overview

The magic link authentication system provides passwordless login through email verification. Users receive a time-limited link that authenticates them without requiring password entry.

Key Security Features:

  • Time-limited tokens (15-minute expiry)
  • Rate limiting (5 attempts per 15 minutes per IP)
  • Domain-based authorization
  • Secure session cookies (HttpOnly, SameSite=strict)
  • Token-based verification with expiry validation

Authentication Modes:

  • Development: Auto-login for any email (testing)
  • Auto-Login List: Instant login for configured emails (admin access)
  • Standard: Magic link sent via email (default production flow)

Technical Implementation

1. Login Request (/login)

File: src/routes/login/+page.server.ts

  • Email format validation with regex
  • Rate limiting tracked by client IP address
  • Domain authorization check against ALLOWED_DOMAINS set
  • Auto-login bypass for configured emails

2. Token Generation

Function: generateMagicToken()

  • Creates JSON object with email and expiry timestamp
  • Encodes to base64url format for URL safety
  • 15-minute expiry from generation time
  • No database storage - stateless token

3. Email Delivery

Service: src/lib/services/email.service.server.ts

  • Google Workspace SMTP via Nodemailer
  • Professional HTML email template
  • Plain text fallback included
  • Connection pooling for performance

4. Token Verification (/verify)

File: src/routes/verify/+page.server.ts

  • Base64 decode and JSON parse validation
  • Timestamp comparison for expiry check
  • Email verification against token payload
  • Session cookie creation with security flags

5. Session Management

  • Cookie Name: iris_session
  • Value: User's email address (lowercase)
  • Security: HttpOnly, SameSite=strict, Secure (production)
  • Duration: 7 days maximum age

Configuration

File: src/routes/quote/config.ts

  • ALLOWED_DOMAINS: Set of authorized email domains
  • AUTO_LOGIN_EMAILS: Set of emails bypassing magic link
  • Rate Limits: 5 attempts per 15-minute window