English
Setup Auth0 on your React app in under 5min
SDK Setup Wizard
npx auth0 init
Create a new project
npm create vite@latest auth0-react -- --template react-ts
cd auth0-react
Install the Auth0 React SDK
npm add @auth0/auth0-react && npm install
Setup your Auth0 App
AUTH0_APP_NAME="My 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 .env
Add the provider
import { StrictMode } from 'react'; import { createRoot } from 'react-dom/client'; import './index.css'; // Importing the main CSS file import App from './App.tsx'; import { Auth0Provider } from '@auth0/auth0-react'; createRoot(document.getElementById('root')!).render( <StrictMode> <Auth0Provider domain={import.meta.env.VITE_AUTH0_DOMAIN} clientId={import.meta.env.VITE_AUTH0_CLIENT_ID} authorizationParams={{ redirect_uri: window.location.origin }} > <App /> </Auth0Provider> </StrictMode> );
Create Login, Logout and Profile Components
touch src/LoginButton.tsx && touch src/LogoutButton.tsx && touch src/Profile.tsx
import { useAuth0 } from "@auth0/auth0-react"; import React from "react"; const LoginButton = (): JSX.Element => { const { loginWithRedirect } = useAuth0(); return <button onClick={() => loginWithRedirect()} className="button login">Log In</button>; }; export default LoginButton;
Run your app
npm run dev