Auth0 Authentication Guide

This guide will help you set up Auth0 for secure user authentication and authorization.

Create an Auth0 Account

Sign up for Auth0 if you haven't already
  1. Visit auth0.com
  2. Click "Sign up" and choose "Sign up for free"
  3. Fill in your information and verify your email
  4. Choose your region (closest to your users)
  5. Complete the onboarding questions

Create a New Application

Set up a Regular Web Application for your React app

1. Navigate to Applications

In your Auth0 dashboard, go to Applications → Applications

2. Create Application

  • Click "Create Application"
  • Name: "Your App Name" (or your preferred name)
  • Type: "Regular Web Application"
  • Click "Create"

Configure Auth0 Application Settings

Set up URLs in Auth0 dashboard and get your credentials

1. Basic Information

Domain: Copy this (e.g., your-tenant.us.auth0.com)
Client ID: Copy this long string
Client Secret: Copy this long string

2. Application URIs

Allowed Callback URLs:
http://localhost:3000, https://yourdomain.com
Allowed Logout URLs:
http://localhost:3000, https://yourdomain.com

3. Save Changes

Scroll down and click "Save Changes"

Set Up Environment Variables

Add your Auth0 credentials to your project

Add to your .env file:

# Auth0 Configuration
AUTH0_DOMAIN=your-tenant.us.auth0.com
AUTH0_CLIENT_ID=your_client_id_here
AUTH0_CLIENT_SECRET=your_client_secret_here
AUTH0_SECRET=your_secret_here

Customize Login Experience

Optional: Customize the Auth0 Universal Login page

1. Universal Login

Go to Branding → Universal Login to customize:

  • Logo and colors
  • Custom CSS
  • Background

2. Social Connections (Optional)

Enable social logins in Authentication → Social

  • Google
  • GitHub
  • LinkedIn
  • And many others

Protect Routes and Pages

Protect routes and pages with Auth0

You can protect routes and pages with Auth0 by using the auth0 instance from lib/auth0. Here's how to implement authentication checks in your pages:

  1. Use middleware to protect routes and pages (see example middleware.ts).
  2. Use the auth0 instance to get the session and user data (see example app/layout.tsx).
  3. Use the auth0 instance to redirect to the login page if the user is not authenticated (see example app/dashboard/layout.tsx).

Test Your Integration

Verify authentication is working
  1. Start your development server:pnpm dev
  2. Click the login button in your app
  3. You should be redirected to Auth0's login page
  4. Create a test account or login
  5. You should be redirected back to your app, now logged in
  6. Check the browser's developer tools to see the user object or console.log(session.user) in the root layout.tsx file and check the console for the user object