Back to Open Source

loginradius-react

License
npm

Table of Contents

Installation

Using npm

1npm install loginradius-react

Getting Started

Configure the SDK by wrapping your application in LRAuthProvider:

1// src/index.js 2import React from "react"; 3import ReactDOM from "react-dom"; 4import App from "./App"; 5import { LRAuthProvider } from "loginradius-react"; 6 7ReactDOM.render( 8 <React.StrictMode> 9 <LRAuthProvider 10 appName="YOUR_LOGINRADIUS_APPNAME" 11 apiKey="YOUR_LOGINRADIUS_APIKEY" 12 redirectUri={window.location.origin} 13 > 14 <App /> 15 </LRAuthProvider> 16 </React.StrictMode>, 17 document.getElementById("root") 18);

Use the useLRAuth hook in your components to access authentication state (isLoading, isAuthenticated and user) and authentication methods (loginWithRedirect and logout):

1// src/App.js 2import React from "react"; 3import { useLRAuth } from "loginradius-react"; 4 5function App() { 6 const { isLoading, isAuthenticated, error, user, loginWithRedirect, logout } = 7 useLRAuth(); 8 9 if (isLoading) { 10 return <div>Loading...</div>; 11 } 12 if (error) { 13 return <div>Oops... {error.message}</div>; 14 } 15 16 if (isAuthenticated) { 17 return ( 18 <div> 19 Hello {user.name}{" "} 20 <button onClick={() => logout({ returnTo: window.location.origin })}> 21 Log out 22 </button> 23 </div> 24 ); 25 } else { 26 return <button onClick={() => loginWithRedirect()}>Log in</button>; 27 } 28} 29 30export default App;

Protect a Route

Protect a route component using the withAuthenticationRequired higher order component. Visits to this route when unauthenticated will redirect the user to the login page and back to this page after login:

1import React from "react"; 2import { withAuthenticationRequired } from "loginradius-react"; 3 4const PrivateRoute = () => <div>Private</div>; 5 6export default withAuthenticationRequired(PrivateRoute, { 7 // Show a message while the user waits to be redirected to the login page. 8 onRedirecting: () => <div>Redirecting you to the login page...</div>, 9});

Call an API

Call a protected API with an Access Token:

1import React, { useEffect, useState } from "react"; 2import { useLRAuth } from "loginradius-react"; 3 4const CallAPI = () => { 5 const { getAccessTokenSilently } = useLRAuth(); 6 const [resp setResp] = useState(null); 7 8 useEffect(() => { 9 (async () => { 10 try { 11 const token = await getAccessTokenSilently(); 12 const response = await fetch( 13 `https://api.loginradius.com/identity/v2/auth/access_token/validate?access_token=${token}&apiKey=${process.env.REACT_APP_API_KEY}`, 14 {} 15 ); 16 setResp(await response.json()); 17 } catch (e) { 18 console.error(e); 19 } 20 })(); 21 }, [getAccessTokenSilently]); 22 23 if (!resp) { 24 return <div>Loading...</div>; 25 } 26 27 return ( 28 <span>{JSON.stringify(state.apiMessage, null, 2)}</span> 29 ); 30}; 31 32export default CallAPI;

Using loginWithPopup

To implement loginWithPopup functionality in your app you need to perform following steps:

  1. Open your LoginRadius Admin Console and navigate to Identity Experience Framework (IDX) -> Themes -> Customize -> Switch to Advance Editor.
  2. In the right side you will find the tab called Custom JS, expand and select Add New.
  3. A popup will open where you need to enter this url in the Url tab - https://hosted-pages.lrinternal.com/Themes/sdk/default-auth-react-sdk.js and click Confirm
  4. This URL contains the script which will help to close the popup after the authentication is done.
  5. Finally Click on Save and then add loginWithPopup method in your application.

Contributing

We appreciate feedback and contribution to this repo! Before you get started, please see the following:

Support + Feedback

For support or to provide feedback, please raise an issue on our issue tracker.

What is LoginRadius

  • LoginRadius was established with the vision of securing the identity of every person on the internet. Over a few short years, we have grown exponentially from a simple social login provider to a multi-faceted, industry-leading customer identity and access management (CIAM) platform.
  • The LoginRadius Identity Platform helps companies securely manage customer identities and data to deliver a unified customer experience. We offer suites of modules to serve businesses from startups to Fortune 500 enterprises with hundreds of millions of users.

License

This project is licensed under the MIT license. See the LICENSE file for more info.

Further Documentation

Find out the documentation for all the commands supported in LoginRadius CLI.
Our Docs