This guide will walk you through integrating MonoCloud with a React application using the oidc-react
library.
Following are the general steps required to integrate MonoCloud into a React application. Refer to the detailed explanation for each step to learn more.
oidc-react
library in your React project.Header.tsx
, which handles user authentication using the oidc-react
library's React hook.App.tsx
, with the AuthProvider component, specifying the OIDC client settings.To get started with integrating authentication in your React.js application, you'll need to:
To use MonoCloud, you need to set up a client in the MonoCloud Dashboard. A client in MonoCloud represents an application within a Project and includes essential configuration details required for integrating that application with MonoCloud.
Follow the steps below to add a new client to the MonoCloud Dashboard:
Once you've added a new client in MonoCloud, you'll need to configure the following settings to integrate your application with MonoCloud:
Follow the steps below to configure these settings in MonoCloud:
+
button.http://localhost:3000
to test the configuration locally.
+
button.http://localhost:3000
to test the configuration locally.
+
button.http://localhost:3000
to test the configuration locally.
openid
, profile
, and email
Identity scopes.To create a React app, follow these steps:
Open your terminal and create a new project using Vite by executing one of the following commands, depending on your package manager:
npm create vite@latest quickstart -- --template react-ts
This creates a new Vite project with the given name and uses the react-ts
template to create a React application using Typescript.
In this example we will use the oidc-react
library to authenticate users in your application. You must install the oidc-react
library in your react project by following the steps below:
cd quickstart
oidc-react
library in your project, by executing the following command:npm install oidc-react
Follow the steps below to configure the development server to run on port 3000:
vite.config.ts
.vite.config.ts
file:import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
+ server: {
+ port: 3000,
+ },
});
To ensure successful integration of MonoCloud in your app, you must set up the AuthProvider
component from oidc-react
in your React application.
The AuthProvider
component requires the values for the following variables:
authority
and client_Id
: These properties should match the Tenant Domain and Client ID found in the General Settings section of your registered client.To retrieve these values, follow the steps below:
Follow the steps below to configure the AuthProvider
component:
Header.tsx
under the src
folder and paste the following code:import { useAuth } from 'oidc-react';
const Header = () => {
const auth = useAuth();
return (
<div style={{ padding: '40px' }}>
{auth.userData ? (
<>
<h3>Logged in as {auth.userData?.profile.email} 🎉</h3>
<button
onClick={() =>
auth.signOutRedirect({ id_token_hint: auth.userData?.id_token })
}
>
Log out!
</button>
</>
) : (
<>
<h3>Not Logged in!</h3>
<button onClick={() => auth.signIn()}>Log In!</button>
</>
)}
</div>
);
};
export default Header;
The code creates a React functional component Header
that uses the oidc-react
library's useAuth
hook to get the current user's session and display the user's current state.
App.tsx
file under the src
folder, and update the existing code with the following code.import { useState } from 'react';
+import { AuthProvider, AuthProviderProps } from 'oidc-react';
import reactLogo from './assets/react.svg';
import viteLogo from '/vite.svg';
+import Header from './Header';
import './App.css';
+const oidcConfig: AuthProviderProps = {
+ authority: 'https://<your-domain>',
+ clientId: '<your-client-id>',
+ redirectUri: 'http://localhost:3000',
+ postLogoutRedirectUri: 'http://localhost:3000',
+ scope: 'openid profile email',
+ responseType: 'code',
+ loadUserInfo: true,
+ autoSignIn: false,
+};
function App() {
const [count, setCount] = useState(0);
return (
<>
+ <AuthProvider {...oidcConfig}>
+ <Header />
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount(count => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
+ </AuthProvider>
</>
);
}
export default App;
redirectUri
and postLogoutRedirectUri
is consistent with the value of the Callback URIs and Sign out URIs in MonoCloud.
You are now ready to start your application's development server by running the following command:
npm run dev
You can now access your application from your web browser. Go to http://localhost:3000 to access your application.
When you click the Login button you will be directed to the MonoCloud login page. After completing the sign-up process, you will gain access to the application.
You can view the registered users from the Users section in the MonoCloud Dashboard.
Well done! You have now established secure user authentication in your React project.