ReactJs
This document describes how to integrate Galaxy ID for ReactJs.
Last updated
This document describes how to integrate Galaxy ID for ReactJs.
Last updated
Software Name: Galaxy ID
Software Version: 0.1
No.
Words
Descriptions
1
GID
Galaxy ID
2
SSO
Single Sign-On
4
Client
An application that integrates for GID
Galaxy ID is an SSO system. It provides endpoints that enable clients to authenticate and authorize in many places.
First, create a new React app.
Install keycloak-js
, @react-keycloack/web
, and react-router-dom
Keycloak and @react-keycloak/web
will be used for the React-Keycloak implementation, while React Router DOM will be used for creating our pages. Please note that this tutorial was created using .
Next, add two folders named components
and pages
to your src
folder. Add a Homepage.js
file and a Securedpage.js
file to the pages
folder, and add a Nav.js
file to the components
folder.
Add the following code to the components/Nav.js
file:
And add the following to the pages/Homepage.js
file.
Add the following to the pages/Securedpage.js
file.
Update your App.js
code with the following code.
The frontend file structure should look similar to this:
After the React frontend is completely implemented, the next step is to configure Keycloak in the React project.
Create a file named Keycloak.js
in your src
folder and add the following code to it.
keycloak.json
(will be provided by admin):
Update your App.js
code to this:
This code imports <ReactKeycloakProvider />
and wraps the entire app with the provider. We also added our keycloak.js
file as a prop.
ProtectedRoute
In this step, we will create a protected route that can only be accessed by authenticated users. Create a helpers
folder, and add a PrivateRoute.js
file to it. Add the following code to the helpers/PrivateRoute.js
file.
This code checks if a user trying to access a protected route is authenticated, and either displays the protected route when a user is authenticated, or displays nothing if the user is unauthenticated.
Update the routes in your App.js
file. We wrapped our SecuredPage
route with the protected route we created; this will ensure that the contents of SecuredPage
can only be accessed by authenticated individuals.
We have now added a new login/logout button. The code checks if a user is authenticated and displays the Login button when the user has been authenticated, and a Logout button when the user has not been authenticated.
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.
When you click the Login button, you should be redirected to a Galaxy ID login page.
With the access token, we can get user info with tokenParsed property: keycloak.tokenParsed (recommend).
With RESTful API:
URL: <host>/realms/<realmName>/protocol/openid-connect/userinfo
Method: GET
Header: Authorization: Bearer <access token>
Example request:
Example response:
Keycloak’s JavaScript adapter provides access to some additional properties for securing your application, such as the authenticated
property, which we will be using to check if a user is authenticated. You can in the Keycloak docs.
When the Login button is clicked, it calls Keycloak’s to authenticate the user. When the Logout button is clicked, it calls the to log the user out.