ReactJs
This document describes how to integrate Galaxy ID for ReactJs.
Software Name: Galaxy ID
Software Version: 0.1
Acronyms and Terms
No.
Words
Descriptions
1
GID
Galaxy ID
2
SSO
Single Sign-On
4
Client
An application that integrates for GID
Summary
Galaxy ID is an SSO system. It provides endpoints that enable clients to authenticate and authorize in many places.
Integrate
Setting up our React frontend
First, create a new React app.
Install keycloak-js, @react-keycloack/web, and react-router-dom
npm install --save keycloak-js @react-keycloak/web react-router-domKeycloak 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 React Router v6.
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:

Setting up Keycloak in React
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.
Setting up React ProtectedRoute
ProtectedRouteIn 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.
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 view the other available properties in the Keycloak docs.
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 the Login button is clicked, it calls Keycloak’s Login method to authenticate the user. When the Logout button is clicked, it calls the Logout method to log the user out.
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.

Get user information
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:
Last updated