A Guide To React User Authentication with LoginRadius

This article focuses on helping developers learn how to integrate user authentication in React applications and determine the basic principles of authentication with React.
profile
Versha GuptaFirst published: 2020-12-10Last updated: 2025-06-25
user-authentication-react-application

This guide uses LoginRadius API for authenticating React apps. It provides React developers with a more straightforward way to add user authentication to react apps. To handle a lot of authentication implementation information, LoginRadius offers a high-level API. Using security best practices, now you can protect your response apps while writing less code.

This article focuses on helping developers learn how to integrate user authentication in the React application. Practice the following security principles to improve authentication on React applications:

  • Add user login and user login.

  • User information retrieval.

  • Attach a tab for sign-up.

In the React application, we can easily and quickly add authentication by using LoginRadius. If you already have a customized login/registration page ready, then I'll guide you to the next step of adding authentication in react apps.

Configure LoginRadius for React Application

A new application was created for you when you signed up for LoginRadius. From here, you get some essential information.

  1. API Key How to get API Key?
  2. Sott Work with Sott

Add following LoginRadius JS library into your application

1<script type="text/javascript" src="https://auth.lrcontent.com/v2/LoginRadiusV2.js"></script>

Add Authentication when you have customized Login Page

Add Login to your react application

We will use an API framework to call LoginRadius APIs for authentication. Create a new file LoginPage.js, and add the following code.

1import  React, { useState } from  "react";
2    const  lrconfig = {
3    apiKey:  "*************************", //LoginRadius API key
4    };
5    const  loginradius = {};
6    if (window.LoginRadiusV2) {
7      loginradius = new  window.LoginRadiusV2(lrconfig);
8      loginradius.api.init(lrconfig);
9    }
10    const  LoginButton = () => {
11      const  loginButtonHandler = () => {
12      loginradius.api.login({emailid:  emailValue,
13      password:  passwordValue},
14      (successResponse)=>{
15      //Here you will get the access Token of 
16  console.log(successResponse);
17  },
18  (errors) => {
19  console.log(errors);
20  });
21  }
22  const [emailValue, updateEmailValue] = useState("");
23  const [passwordValue, updatePasswordValue] = useState("");
24  return (
25  &#x3C;React.Fragment>
26  &#x3C;input  type="text"  value={emailValue}  onChange={(e)=>{updateEmailValue(e.target.value)}}  placeholder={"email"}/>
27  &#x3C;input  type="password"  value={passwordValue}  onChange={(e)=>{updatePasswordValue(e.target.value)}}  placeholder={"Password"}  />
28  &#x3C;button  onClick={() =>  loginButtonHandler()}>Log In&#x3C;/button>
29  &#x3C;/React.Fragment>
30  );
31  };
32  export  default  LoginButton;

In the above code, you will see the lrConfig object, which has apikey that you will get from the LoginRadius account. After calling loginradius.api.login, you will get the response in which you will get the access Token. Through this access token, you can get the user profile.

Add Logout to your react application

Create LogoutPage.js file and add following code:

1import React, { useState } from "react";
2const lrconfig = {
3  apiKey: "*************************", //LoginRadius API key
4};
5
6const loginradius = {};
7if (window.LoginRadiusV2) {
8  loginradius = new window.LoginRadiusV2(lrconfig);
9  loginradius.api.init(lrconfig);
10}
11
12const LogoutButton = () => {
13  const token  = '************'; // Access Token that you got after login
14  const logoutButtonHandler = () => {
15    //Note: Call invalidate token api to invalidate the token.**
16    loginradius.api.invalidateToken(
17           token,
18           (successResponse)=>{
19       console.log(successResponse);
20       },
21           (errors) => {
22             console.log(errors);
23           }
24         );
25   }
26  return (
27    &#x3C;React.Fragment>
28   &#x3C;button onClick={() => logoutButtonHandler()}>Logout&#x3C;/button>
29   &#x3C;/React.Fragment>
30   );
31};
32export default LogoutButton;

In the above code, we have called invalidated token api, which expires your access token.

Add Signup to your react application

Create SignupPage.js file and add the following code:

1import  React, { useState } from  "react";
2    const  lrconfig = {
3    apiKey:  "*************************", //LoginRadius API key
4    sott:  "***************************"  //Secure Token for signup functionality
5    };
6    const  loginradius = {};
7    if (window.LoginRadiusV2) {
8      loginradius = new  window.LoginRadiusV2(lrconfig);
9      loginradius.api.init(lrconfig);
10    }
11    const  SignupButton = () => {
12      const  signupButtonHandler = () => {
13      loginradius.api.registration({
14        email: [{ type: "Primary", value: emailValue }],
15              password: passwordValue
16            },
17      (successResponse)=>{
18      //Here you will get the response after registration
19      console.log(successResponse);
20      },
21      (errors) => {
22      console.log(errors);
23      });
24      }
25      const [emailValue, updateEmailValue] = useState("");
26      const [passwordValue, updatePasswordValue] = useState("");
27      return (
28      <React.Fragment>
29      <input  type="text"  value={emailValue}  onChange={(e)=>{updateEmailValue(e.target.value)}}  placeholder={"email"}/>
30      <input  type="password"  value={passwordValue}  onChange={(e)=>{updatePasswordValue(e.target.value)}}  placeholder={"Password"}  />
31      <button  onClick={() =>  signupButtonHandler()}>Log In</button>
32      </React.Fragment>
33      );
34      };
35      export  default  SignupButton;

In the above code, You will get a success/error response after calling the registration api.

Wrap up

The most common authentication use case for a React application gets covered in this tutorial: quick login and logout. However, LoginRadius is an expandable and versatile platform that can help you accomplish much more. In this guide, We have used the LoginRadius API Framework. If you want to incorporate our hosted page into your application, follow this documentation.

Let me know what you think of this tutorial in the comments below. Thanks for reading :)