Ce guide montre comment intégrer Auth0, ajouter l’authentification et afficher les renseignements du profil de l’utilisateur dans une application monopage (SPA) qui utilise Svelte, à l’aide du SDK Auth0 SPA JS.
Ce guide de démarrage rapide est actuellement en bêta. N’hésitez pas à nous faire part de vos commentaires!
Instruction d’IA
Vous utilisez l’IA pour intégrer Auth0 ? Ajoutez ce prompt à Cursor, Windsurf, Copilot, Claude Code ou à votre IDE préféré propulsé par l’IA pour accélérer le développement.
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: # Vérifier si Node.js et npm sont disponibles node --version && npm --versionThen examine the current directory: # Vérifier l'existence d'un projet Svelte 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:Execute the Auth0 setup command for your OS:If MacOS: # Installer Auth0 CLI si ce n'est pas déjà fait brew tap auth0/auth0-cli && brew install auth0 # Configurer l'application Auth0 et générer le fichier .env auth0 qs setup --app --type spa --framework svelte --build-tool vite --name "My App" --port 5173If Windows (PowerShell): # Installer Auth0 CLI si ce n'est pas déjà fait scoop bucket add auth0 https://github.com/auth0/scoop-auth0-cli.git scoop install auth0 # Configurer l'application Auth0 et générer le fichier .env auth0 qs setup --app --type spa --framework svelte --build-tool vite --name "My App" --port 5173This command will automatically:- Authenticate you with Auth0 (prompts for login if needed)- Create a Single Page Application configured for http://localhost:5173- Generate a .env file with VITE_AUTH0_DOMAIN and VITE_AUTH0_CLIENT_IDCRITICAL 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.
Prérequis : Avant de commencer, assurez-vous d’avoir installé les éléments suivants :
Ce guide de démarrage rapide explique comment ajouter l’authentification Auth0 à une application Svelte. Vous créerez une application monopage sécurisée avec des fonctions de connexion, de déconnexion et de profil d’utilisateur à l’aide du SDK Auth0 SPA JS.
1
Créer un nouveau projet
Créez un nouveau projet Svelte pour ce guide de démarrage rapide
La prochaine étape consiste à créer une nouvelle application sur votre locataire Auth0 et à ajouter les variables d’environnement à votre projet.Vous avez trois options pour configurer votre application Auth0 : utiliser l’outil Quick Setup (recommandé), exécuter une commande CLI ou faire la configuration manuellement dans l’Auth0 Dashboard :
Quick Setup (recommandé)
CLI
Dashboard
Créez une application Auth0 et copiez le fichier .env prérempli avec les bonnes valeurs de configuration.
Exécutez la commande suivante à la racine de votre projet pour créer une application Auth0 et générer un fichier .env :
# Installer Auth0 CLI (si ce n'est pas déjà fait)brew tap auth0/auth0-cli && brew install auth0# Configurer l'application Auth0 et générer le fichier .envauth0 qs setup --app --type spa --framework svelte --build-tool vite --name "My App" --port 5173
Cette commande permet de :
Vérifier si vous êtes authentifié(e) (et vous inviter à vous connecter au besoin)
Créer une application Web monopage Auth0 configurée pour http://localhost:5173
Générer un fichier .env avec VITE_AUTH0_DOMAIN et VITE_AUTH0_CLIENT_ID
Avant de commencer, créez un fichier .env à la racine de votre projet
Cliquez sur Applications > Applications > Create Application
Dans la fenêtre contextuelle, saisissez un nom pour votre application, sélectionnez Single Page Web Application comme type d’application, puis cliquez sur Create
Ouvrez l’onglet Settings sur la page Application Details
Remplacez YOUR_AUTH0_APP_DOMAIN et YOUR_AUTH0_APP_CLIENT_ID dans le fichier .env par les valeurs Domaine et ID client du dashboard
Enfin, dans l’onglet Settings de votre page Application Details, configurez les URL suivantes :Allowed Callback URLs:
http://localhost:5173
Allowed Logout URLs:
http://localhost:5173
Allowed Web Origins:
http://localhost:5173
Allowed Callback URLs constituent une mesure de sécurité essentielle pour garantir que les utilisateurs sont redirigés en toute sécurité vers votre application après l’authentification. Sans URL correspondante, le processus de connexion échouera et les utilisateurs verront une page d’erreur Auth0 au lieu d’accéder à votre application.Allowed Logout URLs sont essentielles pour offrir une expérience utilisateur fluide à la déconnexion. Sans URL correspondante, les utilisateurs ne seront pas redirigés vers votre application après la déconnexion et resteront plutôt sur une page Auth0 générique.Allowed Web Origins est essentiel pour l’authentification silencieuse. Sans cette configuration, les utilisateurs seront déconnectés lorsqu’ils actualiseront la page ou reviendront plus tard à votre application.