Skip to content

Migrate Auth0 integration to Astro with TypeScript SDK#59

Merged
shenanigansd merged 1 commit into
mainfrom
claude/peaceful-brahmagupta-or5ixt
Jun 17, 2026
Merged

Migrate Auth0 integration to Astro with TypeScript SDK#59
shenanigansd merged 1 commit into
mainfrom
claude/peaceful-brahmagupta-or5ixt

Conversation

@shenanigansd

Copy link
Copy Markdown
Member

This PR modernizes the authentication system by migrating from a vanilla JavaScript Auth0 integration to a TypeScript-based implementation using the official @auth0/auth0-spa-js SDK within an Astro framework.

Summary

Replaces the legacy Auth0 SPA sample with a production-ready authentication setup that leverages Astro's component model and modern TypeScript practices. The integration now uses environment variables for configuration, implements proper session persistence with refresh tokens, and provides a cleaner developer experience.

Key Changes

  • New Auth Module (src/lib/auth.ts): Centralized authentication logic with functions for login, logout, user retrieval, and page initialization. Implements singleton pattern for the Auth0 client and handles redirect callbacks with app state preservation.

  • Layout Component (src/layouts/BaseLayout.astro): New base layout with navigation bar showing login/logout buttons that toggle based on authentication state. Replaces inline HTML boilerplate across pages.

  • Account Page (src/pages/account.astro): New gated page displaying user profile information (name, email, picture, etc.) with automatic redirect to Auth0 login if not authenticated.

  • Updated Home Page (src/pages/index.astro): Simplified to use the new BaseLayout and conditionally show account link or login prompt based on auth state.

  • Styling (public/css/main.css): Complete redesign with modern CSS for navigation, profile cards, and responsive layout. Replaces minimal utility styles.

  • Configuration:

    • Moved from public/auth_config.json to environment variables (PUBLIC_AUTH0_DOMAIN, PUBLIC_AUTH0_CLIENT_ID)
    • Added .env.example template for local development
    • Added TypeScript environment definitions (src/env.d.ts)
  • Removed Legacy Code: Deleted public/js/app.js (old vanilla JS implementation) and public/auth_config.json

Notable Implementation Details

  • Uses Authorization Code + PKCE flow with refresh token rotation for secure, persistent sessions across page reloads
  • Implements app state preservation to return users to their original page after Auth0 redirect
  • Leverages localStorage for session caching instead of iframe-based flows (better browser compatibility)
  • All profile data rendered with textContent (never innerHTML) to prevent XSS vulnerabilities
  • Auth client bootstrapped once per page load with redirect handling completed before any page script executes

https://claude.ai/code/session_01DSA5hGpmzekUndBQEUiwpK

The Auth0 integration was the unmodified SPA quickstart sample: it loaded the
SDK from a floating CDN URL, fetched config at runtime, and dumped the raw
access token and full user-profile JSON onto the homepage.

- Use the installed @auth0/auth0-spa-js SDK bundled by Astro instead of the CDN
  script, and a shared typed auth module (src/lib/auth.ts) with a client
  singleton, login/logout, and redirect-callback handling
- Enable useRefreshTokens + localStorage cache so the session survives this
  multi-page site's full-page navigations, plus an explicit
  "openid profile email" scope
- Move config to PUBLIC_AUTH0_* env vars (with .env.example) instead of a
  runtime-fetched public/auth_config.json
- Add BaseLayout with shared nav; rewrite the homepage to stop exposing tokens;
  add a gated /account page that renders a profile card via textContent
- Wire login/logout via addEventListener instead of inline onclick globals
- Add the missing @types/node devDependency so `npm run type-check` works

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DSA5hGpmzekUndBQEUiwpK
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
letsbuilda-dev 5195510 Commit Preview URL

Branch Preview URL
Jun 17 2026, 06:56 PM

@shenanigansd shenanigansd merged commit 28badbd into main Jun 17, 2026
6 checks passed
@shenanigansd shenanigansd deleted the claude/peaceful-brahmagupta-or5ixt branch June 17, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants