React Quickstart¶
Welcome to the React Quickstart guide! In this document, you will learn to build a React app, add user login and display user profile information using WSO2 Identity Server.
[//] STEPS_START
Configure an Application in WSO2 Identity Server¶
- Sign into WSO2 Identity Server console and navigate to Applications > New Application.
- Select Single Page Application and complete the wizard popup by providing a suitable name and an authorized redirect URL.
Example
name: is-react
Authorized redirect URL: http://localhost:5173
Note down the following values from the Protocol tab of the registered application. You will need them to configure Asgardeo React SDK.
client-id
from the Protocol tab.- The name of your WSO2 Identity Server organization
Info
The authorized redirect URL determines where WSO2 Identity Server should send users after they successfully log in. Typically, this will be the web address where your app is hosted. For this guide, we'll usehttp://localhost:5173
, as the sample app will be accessible at this URL.
Create a React app using Vite¶
Create (a.k.a scaffold) your new React app using Vite.
npm create vite@latest is-react -- --template react
cd is-react
npm install
npm run dev
yarn create vite@latest is-react -- --template react
cd is-react
yarn install
yarn dev
pnpm create vite@latest is-react -- --template react
cd is-react
pnpm install
pnpm dev
Install @asgardeo/auth-react¶
Asgardeo React SDK provides all the components and hooks you need to integrate WSO2 Identity Server into your app. To get started, simply add the Asgardeo React SDK to the project. Make sure to stop the dev server started in the previous step.
npm install @asgardeo/auth-react
yarn add @asgardeo/auth-react
pnpm add @asgardeo/auth-react
Add <AuthProvider />
to your app¶
The <AuthProvider />
serves as a context provider for user login in the app. You can add the AuthProvider to your app by wrapping the root component.
Add the following changes to the main.jsx
file.
Important
Replace below placeholders with your registered organization name in WSO2 Identity Server and the generatedclient-id
from the app you registered in WSO2 Identity Server.
<your-app-client-id>
https://api.asgardeo.io/t/<your-organization-name>
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import { AuthProvider } from '@asgardeo/auth-react'
createRoot(document.getElementById('root')).render(
<StrictMode>
<AuthProvider
config={ {
signInRedirectURL: 'http://localhost:5173',
signOutRedirectURL: 'http://localhost:5173',
clientID: '<your-app-client-id>',
baseUrl: 'https://api.asgardeo.io/t/<your-organization-name>',
scope: ['openid', 'profile'],
} }
>
<App />
</AuthProvider>
</StrictMode>
)
Add login and logout link to your app¶
Asgardeo SDK provides useAuthContext
hook to conveniently access user authentication data and sign-in and sign-out methods.
Replace the existing content of the App.jsx
file with following content.
import { useAuthContext } from '@asgardeo/auth-react'
import './App.css'
function App() {
const { state, signIn, signOut } = useAuthContext();
return (
<>
{state.isAuthenticated ? (
<button onClick={() => signOut()}>Logout</button>
) : (
<button onClick={() => signIn()}>Login</button>
)}
</>
)
}
export default App
Visit your app's homepage at http://localhost:5173.
Important
You need to create a test user in WSO2 Identity Server by following this guide to tryout login and logout features.
Display logged in user details¶
Modify the code as below to see logged in user details.
import { useAuthContext } from '@asgardeo/auth-react'
import './App.css'
function App() {
const { state, signIn, signOut } = useAuthContext();
return (
<>
{state.isAuthenticated ? (
<>
<p>Welcome {state.username}</p>
<button onClick={() => signOut()}>Logout</button>
</>
) : (
<button onClick={() => signIn()}>Login</button>
)}
</>
)
}
export default App
[//] STEPS_END