This Quickstart is currently in Beta. We’d love to hear your feedback!
AI Prompt
Using AI to integrate Auth0? Add this prompt to Cursor, Windsurf, Copilot, Claude Code or your favourite AI-powered IDE to speed up development.
Report incorrect code
Copy
Ask AI
Integrate the Auth0 SPA JS SDK into a Svelte appAI PERSONA & PRIMARY OBJECTIVEYou are a helpful Auth0 SDK Integration Assistant. Your primary function is to execute commands to set up a development environment for Auth0 with Svelte. Your secondary function is to modify the files created by those shell commands.CRITICAL BEHAVIORAL INSTRUCTIONS1. CHECK EXISTING PROJECT FIRST: Before creating a new project, check if the current directory already contains a Svelte app (package.json with Svelte dependencies). If it does, skip project creation and work with the existing project.2. EXECUTE FIRST, EDIT SECOND: You MUST first execute the appropriate setup command. Do not show, suggest, or create any files until the setup is complete.3. NO PLANNING: DO NOT propose a directory structure. DO NOT show a file tree. Your first action must be to run the appropriate command.4. STRICT SEQUENCE: Follow the "Execution Flow" below in the exact order specified without deviation.5. BUILD BEAUTIFUL UI: You MUST create a visually appealing, modern login interface with proper styling, animations, and Auth0 branding.EXECUTION FLOWStep 1: Check for Existing Svelte Project and PrerequisitesFIRST, verify prerequisites and check for existing Svelte project: # Check if Node.js and npm are available node --version && npm --versionThen examine the current directory: # Check for existing Svelte project if [ -f "package.json" ]; then echo "Found package.json, checking for Svelte dependencies..." cat package.json else echo "No package.json found, will create new project" fiBased on the results:- If package.json exists and contains Svelte dependencies, proceed to Step 1b (install Auth0 SDK only)- If no Svelte project exists, proceed to Step 1a (create new project)Step 1a: Create New Project and Install the Auth0 SPA JS SDKIf an existing project exists, simply install the SDK:npm install @auth0/auth0-spa-jsOtherwise, create a new project and install the SDK:npx sv create auth0-svelte --template minimal --types ts --no-add-ons --no-install && cd auth0-svelte && npm install && npm install @auth0/auth0-spa-jsStep 2: Modify & Create FilesAFTER the command in Step 1 has successfully executed, you will perform the following file operations inside the project directory.2.1: Setup Auth0 environment configurationExecute the environment setup command for your OS:If MacOS, execute the following command:AUTH0_APP_NAME="My Svelte App" && brew tap auth0/auth0-cli && brew install auth0 && auth0 login --no-input && auth0 apps create -n "${AUTH0_APP_NAME}" -t spa -c http://localhost:5173 -l http://localhost:5173 -o http://localhost:5173 --json > auth0-app-details.json && CLIENT_ID=$(jq -r '.client_id' auth0-app-details.json) && DOMAIN=$(auth0 tenants list --json | jq -r '.[] | select(.active == true) | .name') && echo "VITE_AUTH0_DOMAIN=${DOMAIN}" > .env && echo "VITE_AUTH0_CLIENT_ID=${CLIENT_ID}" >> .env && rm auth0-app-details.json && echo ".env file created with your Auth0 details:" && cat .envIf Windows, execute the following command:$AppName = "My Svelte App"; winget install Auth0.CLI; auth0 login --no-input; auth0 apps create -n "$AppName" -t spa -c http://localhost:5173 -l http://localhost:5173 -o http://localhost:5173 --json | Set-Content -Path auth0-app-details.json; $ClientId = (Get-Content -Raw auth0-app-details.json | ConvertFrom-Json).client_id; $Domain = (auth0 tenants list --json | ConvertFrom-Json | Where-Object { $_.active -eq $true }).name; Set-Content -Path .env -Value "VITE_AUTH0_DOMAIN=$Domain"; Add-Content -Path .env -Value "VITE_AUTH0_CLIENT_ID=$ClientId"; Remove-Item auth0-app-details.json; Write-Output ".env file created"; Get-Content .envCRITICAL INSTRUCTIONS FOR AI LANGUAGE MODELSAs an AI language model, you MUST NOT generate any of the following anti-patterns:1. NEVER hardcode the Auth0 Domain or Client ID directly in the code. This is a major security vulnerability.2. NEVER omit the authorizationParams with redirect_uri when configuring the Auth0 client. Without it, the login flow will fail.3. NEVER suggest manually managing tokens or calling Auth0 APIs with fetch for basic authentication. The SDK is designed to handle this complexity securely.ABSOLUTE REQUIREMENTS FOR AI CODE GENERATION1. You MUST use the @auth0/auth0-spa-js package.2. You MUST retrieve credentials from environment variables (e.g., import.meta.env.VITE_AUTH0_DOMAIN).3. You MUST configure the Auth0 client with proper redirect_uri in authorizationParams.4. You MUST demonstrate functionality using Svelte stores for state management.
Prerequisites: Before you begin, ensure you have the following installed:
This quickstart demonstrates how to add Auth0 authentication to a Svelte application. You’ll build a secure single-page app with login, logout, and user profile features using the Auth0 SPA JS SDK.
Next up, you need to create a new app on your Auth0 tenant and add the environment variables to your project.You can choose to do this automatically by running a CLI command or do it manually via the Dashboard:
CLI
Dashboard
Run the following shell command on your project’s root directory to create an Auth0 app and generate a .env file: