FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
project logo

stevekrouse

luciaMagicLinkStarter

Demo app of roll-your-own auth & magic link login
Remix of std/reactHonoStarter
Public
Like
7
luciaMagicLinkStarter
Home
Code
3
backend
3
frontend
3
README.md
Branches
1
Pull requests
Remixes
4
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
README.md
Code
/
README.md
Search
5/8/2025
Viewing readonly version of main branch: v146
View latest version
README.md

Lucia Auth Starter

A demonstration of magic link authentication using Val Town.

Features

  • Magic link authentication
  • Server-side rendering with client-side hydration
  • User profile management (username editing with validation)
  • Session management with secure cookies and token rotation
  • Responsive UI with Tailwind CSS

Authentication Flow

  1. User enters their email address on the login page
  2. A magic link is sent to the user's email
  3. User clicks the magic link in their email
  4. The server validates the token, creates a session, and sets a secure cookie
  5. User is redirected to the authenticated home page

Technical Implementation

  • Database: SQLite for storing users, sessions, and magic link tokens
  • Frontend: React with Tailwind CSS
  • Backend: Hono.js for API routes and middleware
  • Authentication: Custom implementation with secure cookies
  • Session Security: Sliding + rotating sessions (new token generated when 50% of session lifetime remains)
  • Input Validation: Username validation enforces 3-50 printable characters and prevents XSS

API Endpoints

  • POST /auth/magic-link - Request a magic link
  • GET /auth/magic-link/:token - Validate a magic link token
  • POST /auth/logout - Log out the current user
  • POST /api/user/username - Update the user's username

Notes

  • Email sending requires Val Town Pro subscription for sending to addresses other than your own
  • Magic links expire after 15 minutes
  • Sessions expire after 30 days
  • Session tokens are automatically rotated when 50% of their lifetime remains (15 days)
  • Username validation rules: 3-50 printable characters, no "</>" sequences to prevent XSS

Todos

  • When I tried to make the magic link token single-use, it caused issues. I think the best practice there is something a bit more complicated, ie a GET request to an HTML page that makes a POST request.
  • When I tried to make the SameSite=Strict, the cookie stopped working. It's currently Lax, which I think is OK, but it'd be nice to know why.
FeaturesVersion controlCode intelligenceCLI
Use cases
TeamsAI agentsSlackGTM
ExploreDocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareersBrandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.