Learn how to quickly add authentication and user profile in your React app using Hanko.
To setup our React Frontend we will use vite as a build tool.
Run the following command to create a new Vite React application:
@teamhanko/hanko-elements
Once you’ve initialized your React app, installing hanko-elements provides you with access to the prebuilt components: hanko-auth
and hanko-profile
.
We will also install the react-router-dom
, this will allow us to navigate trough pages / routes.
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:5173/)
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.
Create a folder called components
and create two files in it, HankoAuth.tsx
and HankoProfile.tsx
.
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.
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.
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.
After you created the LoginPage.tsx
and DashboardPage.tsx
you are able to import them into your react App.tsx
.
We will use react-router-dom
to setup the routes of your app.
By now you should be able to go to /
to see the <HankoAuth>
, and to /dashboard
to see the <HankoProfile>
.
They should look something like this👇
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.
You can customize the appearance of hanko-auth
and hanko-profile
components using CSS variables and parts. Refer to our customization guide.
To secure our routes we should validate the session token at the backend. Please refer to our backend guides.
Lets set up a Private route to do this for us.
Create a new React component at components/PrivateRoute.tsx
.
If the backend couldn’t validate our token we get navigated back to /
.
Otherwise if the validation was successfull it will return the children inside the private route.
Lets import this PrivateRoute.tsx
to your App.tsx
file.
To use the private route wrap your Dashboard
in the Private Route;
To verify that it works, logout on your app and go to /dashboard
, you should get redirected back.
Lets use the Hanko SDK to get user data.
Lets update the dashboard
page to log some of the information from the user.
It uses Express.js for the backend, full source code available on our GitHub.
Learn how to quickly add authentication and user profile in your React app using Hanko.
To setup our React Frontend we will use vite as a build tool.
Run the following command to create a new Vite React application:
@teamhanko/hanko-elements
Once you’ve initialized your React app, installing hanko-elements provides you with access to the prebuilt components: hanko-auth
and hanko-profile
.
We will also install the react-router-dom
, this will allow us to navigate trough pages / routes.
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:5173/)
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.
Create a folder called components
and create two files in it, HankoAuth.tsx
and HankoProfile.tsx
.
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.
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.
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.
After you created the LoginPage.tsx
and DashboardPage.tsx
you are able to import them into your react App.tsx
.
We will use react-router-dom
to setup the routes of your app.
By now you should be able to go to /
to see the <HankoAuth>
, and to /dashboard
to see the <HankoProfile>
.
They should look something like this👇
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.
You can customize the appearance of hanko-auth
and hanko-profile
components using CSS variables and parts. Refer to our customization guide.
To secure our routes we should validate the session token at the backend. Please refer to our backend guides.
Lets set up a Private route to do this for us.
Create a new React component at components/PrivateRoute.tsx
.
If the backend couldn’t validate our token we get navigated back to /
.
Otherwise if the validation was successfull it will return the children inside the private route.
Lets import this PrivateRoute.tsx
to your App.tsx
file.
To use the private route wrap your Dashboard
in the Private Route;
To verify that it works, logout on your app and go to /dashboard
, you should get redirected back.
Lets use the Hanko SDK to get user data.
Lets update the dashboard
page to log some of the information from the user.
It uses Express.js for the backend, full source code available on our GitHub.