Skip to main content

Create a Next.js application

Run the following command to create a new Next.js application:
When setting up Next.js, you’ll choose either the App Router or the Pages Router. Be sure to follow the proper directory structure and add the matching code for the router you selected.
This creates a new Next.js application using the latest version and default settings.

Install @teamhanko/hanko-elements

Once you’ve initialized your NextJS app, installing hanko-elements provides you with access to the prebuilt components: hanko-auth and hanko-profile. This installs the Hanko Elements package, providing pre-built authentication components.

Set up your Hanko project

Go to the Hanko Console and create a project for this application.
During creation make sure to input the URL you will be developing on as the APP URL. (Most likely http://localhost:3000)

Add your Hanko API URL

Retrieve your API URL from the Hanko Console, and paste this in a .env file. Set up your environment variable for the Hanko API URL. The NEXT_PUBLIC_ prefix makes it accessible in the browser.
.env.local
If you are self-hosting you need to provide the URL of your running Hanko backend.

Create Hanko components

Create a folder called components and create two files, HankoAuth.tsx and HankoProfile.jsx.

Add Typescript types

To get these elements to work with typescript, currently we must add the types to the project. To do do this, create a file called custom.d.ts and place it in your apps root / src folder. Add TypeScript declarations for Hanko Web Components to enable type checking and IntelliSense support.
src/custom.d.ts

Login page with <hanko-auth>

Now lets setup the HankoAuth.tsx file to create a functioning login page. Here we subscribe to the onSessionCreated event, this triggers when a user successfully logs in. You can use these event to perform any desired action. (e.g. redirect to your dashboard). For more information please refer to the Auth component page.
App Router version: This authentication component handles session events and redirects users after successful login.
components/HankoAuth.tsx
Now simply import the component you just created.
App Router login page that renders the HankoAuth component.
app/login/page.tsx
By now, your sign-up and sign-in features should be working. You should see an interface similar to this 👇
sign up

Profile page with <hanko-profile>

After setting up HankoAuth, let’s set up the HankoProfile.jsx file to create an interface where users can manage their email addresses and login methods. For more information please refer to the Profile component page.
App Router profile component for managing user emails and credentials.
components/HankoProfile.jsx
After you created the HankoProfile component, simply import it into any page.
app/dashboard/page.tsx
It should look something like this 👇
profile page

Implement logout functionality

You can use @teamhanko/hanko-elements to easily logout users. Here we will make a logout button. Create LogoutButton.tsx and insert the code below.
App Router logout button component that handles session cleanup and navigation.
components/LogoutButton.tsx

Customize component styles

You can customize the appearance of hanko-auth and hanko-profile components using CSS variables and parts. Refer to our customization guide.

Securing routes with middleware

To verify the session token in your Next.js application, we’re using the session/validate API request. By checking for a valid session token this middleware will ensure secure access to specific routes, like /dashboard and /protected.
The middleware extracts and verifies the session token, and redirect unauthorized users back to the home or login page.
For more info on middlewares and where to put the middleware.ts file,
please refer to NextJS Middleware.
Middleware tends to not always work after creating it, if this is the case try restarting your next app.
This Next.js middleware protects routes by validating Hanko session tokens.
middleware.ts
To verify that it works, logout on your app and go to /dashboard, you should get redirected back.

Getting user data

Client side

Lets use the Hanko SDK to get user data. Lets update the dashboard page to log some of the information from the user and session.
App Router dashboard with client-side user data retrieval using the Hanko SDK.
app/dashboard/page.tsx

Server side

On the server side, you can extract the userID from the session token, which you can use to fetch the user’s data from the Hanko Public API. This server-side function validates sessions and fetches user data using the Hanko API.
getUserData.ts

Try it yourself

Next.js example (App router)

Full source code available on our GitHub

Next.js example (Pages router)

Full source code available on our GitHub.