Loading...
Loading...
Use when adding authentication to Vanilla JS, Svelte, or any framework-agnostic single-page applications - integrates @auth0/auth0-spa-js SDK for SPAs without framework-specific wrappers
npx skill4agent add auth0/agent-skills auth0-spa-js@auth0/auth0-spa-jsAgent instruction: Before providing SDK setup instructions, fetch the latest release version by running:gh api repos/auth0/auth0-spa-js/releases/latest --jq '.tag_name'Use the returned version in all dependency lines instead of any hardcoded version below. If the command fails, fall back to checking https://github.com/auth0/auth0-spa-js/releases.
npm install @auth0/auth0-spa-jsVERSION=$(npm view @auth0/auth0-spa-js version)<script src="https://cdn.auth0.com/js/auth0-spa-js/$VERSION/auth0-spa-js.production.js"></script>.envVITE_AUTH0_DOMAIN=your-tenant.auth0.com
VITE_AUTH0_CLIENT_ID=your-client-idhttp://localhost:5173http://localhost:5173http://localhost:5173import { createAuth0Client } from '@auth0/auth0-spa-js';
const auth0 = await createAuth0Client({
domain: import.meta.env.VITE_AUTH0_DOMAIN,
clientId: import.meta.env.VITE_AUTH0_CLIENT_ID,
authorizationParams: {
redirect_uri: window.location.origin
}
});
// Handle redirect callback after login
const query = new URLSearchParams(window.location.search);
if ((query.has('code') || query.has('error')) && query.has('state')) {
await auth0.handleRedirectCallback();
window.history.replaceState({}, document.title, window.location.pathname);
}Agent instruction: Before adding new UI elements, search the project for existing click handlers for login, logout, sign-in, or sign-out buttons. If existing handlers are found, hook the Auth0 code into them without modifying the existing UI. Only create new buttons if no existing handlers are found.
// Login
document.getElementById('login-btn').addEventListener('click', async () => {
await auth0.loginWithRedirect();
});
// Logout
document.getElementById('logout-btn').addEventListener('click', () => {
auth0.logout({
logoutParams: { returnTo: window.location.origin }
});
});
// Update UI based on auth state
const isAuthenticated = await auth0.isAuthenticated();
if (isAuthenticated) {
const user = await auth0.getUser();
console.log(user.name, user.email);
}const accessToken = await auth0.getTokenSilently();
const response = await fetch('https://your-api.example.com/data', {
headers: { Authorization: `Bearer ${accessToken}` }
});Agent instruction: After completing the integration, build the project to verify it compiles successfully:bashnpm run buildIf the build fails, analyze the error output and fix the issues. Common integration build failures include:
- Module not found: Missing
— run the install commandnpm install @auth0/auth0-spa-js- Cannot find name 'import.meta': TypeScript target too low — set
or higher in"target": "ES2020"tsconfig.json is not a function: Wrong import path or CDN usage without bundle stepcreateAuth0Client- Env vars undefined at runtime: Vite requires
prefix; webpack/CRA requiresVITE_prefixREACT_APP_Re-run the build after each fix. Track the number of build-fix iterations.Failcheck: If the build still fails after 5–6 fix attempts, stop and ask the user using: "The build is still failing after several fix attempts. How would you like to proceed?"AskUserQuestion
- Let the skill continue fixing iteratively — continue the build-fix loop for another 5–6 attempts
- Fix it manually — show the remaining errors and let the user resolve them
- Skip build verification — proceed without a successful build
.env| Mistake | Fix |
|---|---|
Callback URL port mismatch (e.g., | Match Allowed Callback URLs exactly to your dev server port in Auth0 Dashboard |
| SPAs must never have a client secret — remove it. Auth0 sets auth method to |
Tokens stored in | Use in-memory storage (default) or |
| Add your app origin to Allowed Web Origins in Auth0 Dashboard |
| Must call after login redirect to exchange the auth code; without this the URL params persist and re-trigger |
Domain includes | Auth0 domain should be hostname only: |
| Popups must be triggered directly from a user gesture (click handler). Never call from init or page load code |
Using | For Vanilla JS, use |
| Method | Description |
|---|---|
| Create and initialize client (calls |
| Instantiate without auto session check |
| Redirect to Auth0 Universal Login |
| Open Auth0 login in a popup |
| Clear session and redirect |
| Process redirect result after login |
| |
| |
| |
| Attempt silent re-authentication |