How to Add Authentication to an Astro App (2026 Guide)

Astro Auth

Complete guide to Astro auth. Covers Better Auth, Clerk, Supabase Auth, and SSR patterns for Astro 5 and 6 apps.

Astro is the fastest-growing web framework for content sites, with adoption doubling every year. In January 2026, Cloudflare acquired the Astro team, and Astro 6 introduced first-class Cloudflare Workers support. Major brands including Porsche, IKEA, Visa, and OpenAI use Astro in production. And 60% of Astro sites score “Good” on Core Web Vitals, compared to 38% for WordPress and Gatsby sites.

But Astro started as a static site generator, and authentication requires a server. This guide covers how to add auth to Astro apps in SSR and hybrid modes, with every practical approach compared.

How Astro Auth Architecture Works

Astro was designed to ship minimal JavaScript. Unlike React or Vue SPAs, Astro pages render on the server and send HTML to the browser. This creates a natural advantage for auth: session validation happens server-side before any HTML reaches the client.

Authentication in Astro requires SSR mode. Three configuration options exist:

  • output: 'server' — Every page renders on the server. All routes can check auth.
  • output: 'hybrid' — Pages are static by default, but individual pages can opt into SSR with export const prerender = false. Use this when only some routes need auth.
  • output: 'static' — No server at all. Cannot handle auth. You need a separate backend or client-side auth with a managed provider.

The standard Astro auth flow uses middleware:

  • Middleware (src/middleware.ts) runs on every server request. It reads the session cookie, validates the token, and attaches user data to Astro.locals.
  • Page frontmatter accesses Astro.locals.user to gate content. If the user is not authenticated, you return a redirect response.
  • API routes (src/pages/api/) handle login, logout, and registration POST requests.

Astro 5 introduced Server Islands — per-component server rendering within otherwise static pages. This means you can have a static marketing page with an authenticated dashboard widget rendered on the server, mixing static and dynamic content on the same page.

Common Astro Auth Approaches Compared

Better Auth

Better Auth became the primary recommendation for Astro authentication after Lucia Auth deprecated itself in March 2025. It is TypeScript-first, works with your own database, and supports Astro’s Server Islands feature.

What works well:

  • Supports Astro out of the box with official integration
  • Plugin ecosystem: 2FA, passkeys, multi-tenancy, rate limiting
  • Stores auth data in your database (PostgreSQL, MySQL, SQLite)
  • Compatible with Astro 5 Server Islands and Astro 6

Where it falls apart:

  • More setup than managed providers (database, environment variables, adapter configuration)
  • You host and maintain the auth infrastructure
  • Younger project compared to Auth.js or Firebase
  • Documentation is growing but not as comprehensive as Auth0’s

Clerk

Clerk provides managed auth with pre-built components. Their Astro integration handles sign-in, sign-up, and user management through Clerk’s hosted infrastructure.

What works well:

  • Pre-built sign-in and user profile components
  • 10,000 free monthly active users
  • Handles the entire auth lifecycle
  • Works with Astro’s SSR mode

Where it falls apart:

  • Vendor lock-in to Clerk’s API and servers
  • $0.02 per MAU after the free tier
  • Components may not match your site’s design system
  • Requires Clerk’s JavaScript SDK in the client

Supabase Auth

Supabase Auth pairs authentication with a PostgreSQL database and Row Level Security. The @supabase/ssr package handles server-side session management in Astro.

What works well:

  • Free up to 50,000 MAUs
  • Row Level Security ties auth to database permissions
  • Email, phone, and social login included
  • Self-hostable for full data control

Where it falls apart:

  • Ties your auth to Supabase’s infrastructure
  • SSR cookie management requires the @supabase/ssr package
  • No built-in payments or subscription management
  • Session refresh handling adds complexity in SSR mode

Auth.js

Auth.js can be used with Astro through custom integration, though it does not have a dedicated @auth/astro package. You handle the OAuth callbacks and session management in Astro API routes.

What works well:

  • 80+ OAuth providers
  • Well-established library with a large community
  • Database adapters for most ORMs
  • Free and open source

Where it falls apart:

  • No official Astro integration package
  • Requires manual wiring of callback routes and session handling
  • Documentation is Next.js-focused
  • Less ergonomic than Better Auth for Astro specifically

The Real Problem: Auth Is Only Half the Battle

Astro excels at content sites and marketing pages, but more developers are using it for SaaS products with SSR mode. Authentication is the first infrastructure piece. Payments are the second.

Every auth solution above handles login. None handles Stripe checkout, subscription management, or payment webhooks. If you are building a SaaS with Astro, you will need a separate Stripe integration — webhook handlers, customer mapping, entitlement checks.

How Beag Simplifies Astro Auth and Payments

Beag was built with Astro in mind. Add the Beag script tag to your Astro layout and get:

  • Authentication with email, social login, and magic links
  • Stripe payments with checkout, subscriptions, and customer portal
  • User-to-customer mapping handled automatically
  • Session data accessible from Astro.locals or client-side API

Since Astro sends minimal JavaScript to the browser, Beag’s single script tag fits naturally into Astro’s architecture. No heavy client-side SDKs, no framework-specific wrappers. For a step-by-step walkthrough, see our Astro auth blog post.

Choosing the Right Approach

SolutionCostSetup TimeAuth + Payments
Better AuthFree (open source)2-4 hoursAuth only
Clerk$0.02/MAU after 10K30-60 minutesAuth only
Supabase AuthFree up to 50K MAU2-3 hoursAuth only
Auth.js (custom)Free (open source)3-6 hoursAuth only
Beag$19/month15 minutesBoth included

For comparisons with other frameworks, see our Next.js auth guide or the SvelteKit auth guide. Browse all guides in the guide hub.

What to Do Next

  1. Enable SSR mode. Set output: 'server' or output: 'hybrid' in your Astro config.
  2. Pick your auth approach. Better Auth for self-hosted, Clerk for managed.
  3. Consider total cost. Development time matters more than subscription fees.
  4. Start building. Try Beag free for 7 days or explore the docs.

Frequently Asked Questions

Can Astro handle authentication if it is a static site generator?

Yes, but only in SSR or hybrid mode. Astro's static output mode cannot handle auth because there is no server to validate sessions. You need to enable server-side rendering (output: 'server' or output: 'hybrid' in astro.config.mjs) to process cookies, validate tokens, and protect routes on the server.

What is Better Auth and why is it recommended for Astro in 2026?

Better Auth is a TypeScript-first, framework-agnostic auth library that became the recommended option after Lucia Auth deprecated itself in March 2025. It supports Astro out of the box, works with Server Islands in Astro 5, and includes plugins for 2FA, passkeys, and organization management. It stores auth data in your own database.

How does the Cloudflare acquisition affect Astro auth?

Cloudflare acquired Astro's team in January 2026. Astro remains open source. The main impact is first-class Cloudflare Workers support in Astro 6, which means auth middleware runs on Cloudflare's edge network. This reduces authentication latency for global users but means your auth strategy needs to work in a Workers environment.

Should I use Astro middleware or page-level checks for auth?

Use Astro middleware (src/middleware.ts) for auth checks that apply to many routes, like verifying a session cookie and attaching user data to Astro.locals. Use page-level checks in the frontmatter for route-specific logic, like redirecting non-admin users. Middleware runs on every matching request, so keep it efficient.

How long does it take to add auth to an Astro app?

With Clerk, basic auth takes 30-60 minutes. Better Auth setup takes 2-4 hours including database configuration. Supabase Auth with SSR takes 2-3 hours. If you need auth and payments together, Beag handles both in about 15 minutes with a single script tag.

Skip the Auth and Payments Headaches

Beag handles authentication and Stripe payments with a single script tag. Ship your SaaS faster.

Start 7-day free trial