Integrate Hanko with Next.js
Learn how to quickly add authentication and user profile in your Next.js app using Hanko.
Create a Next.js application
Run the following command to create a new Next.js application:
During the Next.js setup you could pick between two routers, the App Router and the Pages Router. Ensure you add the correct code and follow the correct directory structure for Router that you picked.
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
.
Setup 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.
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
.
Typescript
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.
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.
Now simply import the component you just created.
By now, your sign-up and sign-in features should be working. You should see an interface similar to this 👇
Hanko profile
After setting up the HankoAuth let’s set up the HankoProfile.jsx
file to create an interface where users can
manage their Email Addresses
and credentials.
For more information please refer to the Profile Component Page.
After you created the HankoProfile
component, simply import it into any page.
It should look something like this 👇
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.
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.
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.
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.