.env.local
with the following environment variables:AUTH0_SECRET
: A long secret value used to encrypt the session cookie. You can generate a suitable string using openssl rand -hex 32
on the command line.
APP_BASE_URL
: The base URL of your application.
AUTH0_DOMAIN
: The URL of your Auth0 tenant domain. If you are using a Custom Domain with Auth0, set this to the value of your Custom Domain instead of the value reflected in the “Settings” tab.
AUTH0_CLIENT_ID
: Your Auth0 application’s Client ID.
AUTH0_CLIENT_SECRET
: Your Auth0 application’s Client Secret.
src/lib/auth0.ts
. This file provides methods for handling authentication, sessions and user data.Then, import the Auth0Client
class from the SDK to create an instance and export it as auth0
. This instance is used in your app to interact with Auth0.src/middleware.ts
. This file is used to enforce authentication on specific routes.The middleware
function intercepts incoming requests and applies Auth0’s authentication logic. The matcher
configuration ensures that the middleware runs on all routes except for static files and metadata.src/app/page.tsx
is where users interact with your app. It displays different content based on whether the users is logged in or not.Edit the file src/app/page.tsx
to add the auth0.getSession()
method to determine if the user is logged in by retrieving the user session.If there is no user session, the method returns null
and the app displays the Sign up or Log in buttons. If a user sessions exists, the app displays a welcome message with the user’s name and a Log out button.src/app/page.tsx
. When the user selects the Log out button, they are redirected to the Auth0 logout endpoint, which clears their session and redirects back to your app.http://localhost:3000
in your browser.You will see: