Integrate Hanko with Remix
Learn how to quickly add authentication and user profile in your Remix app using Hanko.
Install @teamhanko/hanko-elements
Once you’ve initialized your Remix app, installing hanko-elements provides you with access to the prebuilt components: hanko-auth
and hanko-profile
.
Add the Hanko API URL
Retrieve the API URL from the Hanko console and place it in your .env
file.
If you are self-hosting you need to provide the URL of your running Hanko backend.
Importing and exporting Hanko functions in a .client.ts
file
Since the @teamhanko/hanko-elements
package is designed to work on the client side, you’ll need to import the Hanko functions into a .client.ts
file. This file will then export them, making them available for use throughout your application.
Add <hanko-auth>
component
The <hanko-auth>
web component adds a login interface to your app. The following code example defines a HankoAuth
component in the login
route.
It uses the register function from hanko.client.ts
to register <hanko-auth>
component with the brower’s CustomElementRegistry. When the authentication flow is completed, a callback is set up to redirect the user to the dashboard.
By now, your sign-up and sign-in features should be working. You should see an interface similar to this 👇
Add <hanko-profile>
component
The <hanko-profile>
component provides an interface, where users can manage their email addresses and passkeys.
It should look like this 👇
Implement logout functionality
You can use @teamhanko/hanko-elements
to easily manage user logouts. Below we add a logout button to the dashboard page.
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
To add JWT verification with Hanko in your Remix application, we’re using the jose library. However, you’re free to choose any other suitable library.
Below we define a validateJwtAndFetchUserId
function which validates the JWT and fetches the user ID. This will also be used to protect routes and ensure only authenticated users can access certain parts of the application.
This is how you can use it in your routes:
Try it yourself
Remix example
Full source code available on our GitHub