How To Implement Keycloak Authentication in React - LogRocket Blog
How To Implement Keycloak Authentication in React - LogRocket Blog
BLOG
Introduction
Providing secure user authentication and management can
sometimes be a daunting task when building a modern
application. This is why most developers prefer to o oad the
problem to third-party authentication services like Okta, Auth0,
or Keycloak.
In this tutorial, you will learn how to secure your React app with
Keycloak. Keycloak is an open-source identity and access
management tool for adding authentication to modern
applications and services.
Enjoy!
Getting started
To easily follow through with the tutorial, ensure you have the
following:
Node.js and npm working on your local machine (I created the
tutorial using the latest Node.js stable version, v16.13.0)
Basic knowledge of JavaScript
Docker installed on your local machine
Version: 1.4.11
GitCommit:
5b46e404f6b9f661a205e28d59c982d3634148f8
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
If you don’t have Docker installed locally, you can follow this
complete Docker installation guide to install Docker on your
Windows, macOS, or Linux machine.
After con rming your Docker installation, the next step will be
to set up a Keycloak server. To start up the Keycloak server on
your local machine, run the following command in your
terminal:
After creating a realm, the next step will be to add users to it.
Follow these steps in the admin console:
1. Click Users on the side menu and select Add user in the new
window that appears
2. Fill in the needed details, set Email Veri ed to ON and click Save
to register the changes
3. Click Credentials in the new window that appears, and input and
con rm the user password.
4. Toggle temporary to OFF, and click Set Password to register the
new password.
Let’s test if our new user is working. Log out of the admin
console and visit http://localhost:8080/auth/realms/<realm-
name>/account/ — you should change <realm-name> to the
created user’s realm name.
Click Sign in and enter your new user’s username and password.
After a successful sign-in, you should gain access to a page
similar to this:
Adding your React app to Keycloak
The last step in the Keycloak server setup will be to register the
React frontend app as a client. To do that, we want to log in to
the admin console, navigate to the Clients page in the sidebar
menu, and click Create.
With this, our Keycloak server is fully set up! We can now move
to the frontend and complete our integration.
</a>
</li>
<li>
<a className="hover:text-blue-800"
href="/secured">
Secured Page
</a>
</li>
</ul>
<div className="hidden xl:flex items-center
space-x-5">
<div className="hover:text-gray-200">
<h1>Login</h1>
</div>
</div>
</div>
</nav>
</section>
</div>
</div>
return (
<div>
<h1 className="text-green-800 text-4xl">Welcome to the
Homepage</h1>
</div>
);
};
return (
<div>
<h1 className="text-black text-4xl">Welcome to the
Protected Page.</h1>
</div>
);
};
function App() {
return (
<div>
<Nav />
<BrowserRouter>
<Routes>
<Route exact path="/" element={<WelcomePage />} />
<Route path="/secured" element={<SecuredPage />}
/>
</Routes>
</BrowserRouter>
</div>
);
}
function App() {
return (
<div>
<ReactKeycloakProvider authClient={keycloak}>
<Nav />
<BrowserRouter>
<Routes>
<Route exact path="/" element={<WelcomePage />}
/>
<Route path="/secured" element={<SecuredPage />}
function App() {
return (
<div>
<ReactKeycloakProvider authClient={keycloak}>
<Nav />
<BrowserRouter>
<Routes>
<Route exact path="/" element={<WelcomePage />}
/>
return (
<div>
<div className="top-0 w-full flex flex-wrap">
<section className="x-auto">
<nav className="flex justify-between bg-gray-200
text-blue-800 w-screen">
<div className="px-5 xl:px-12 py-6 flex w-full
items-center">
<h1 className="text-3xl font-bold font-
heading">
Keycloak React AUTH.
</h1>
<ul className="hidden md:flex px-4 mx-auto
font-semibold font-heading space-x-12">
When you visit the demo React website we created, there should
now be a Login button on the navbar. Also, the secured page
should display nothing if you try to access it.
Conclusion
In this tutorial, we implemented a simple login/logout feature
with protected routes in a React app using Keycloak. We
implemented a private route with Keycloak’s authenticated
property, and login/logout functionality with Keycloak’s Login
and Logout methods.
Software engineer
#react
Hi Thank A lot for this tutorial. it’s been very helpful. I am writing
because may be you can help me. After doing it I received this error:
do you know how can I solved it?
Thanks a lot!
The error should be in the app.js le. You have add keycloak’s
authclient to reactkeycloakprovider. Note the reactkaycloakprovider
part in the code below.
function App() {
return (
}
/>
);
}
Thanks
Leave a Reply