Login Authentication in React

Authentication Features in React are primary and widely use features and can be performed in different ways. In React Library we can easily create a function that can authenticate and render components accordingly.  In short, we are gonna save and use the data of logged-in users throughout the different pages of the web application.

Note: – React is a front-end library we will be using any random API to authenticate in the backend and then store the data in states to validate the authentication and use it to render components.

Packages: –

  • React
  • react-router-dom

Create the Reducer 

Reducer is the function the will hook the data to the state and can send data to different pages which are rendered under its Provider. This file is created in the src>reducers folder as per the standard conventions, moreover, reducers can be work with different properties other than authentication. But here we will use it for authentication.

export const initialState = null;

export const reducer = (state,action) =>{
    if(action.type === "USER"){
        return action.payload
    if(action.type === "CLEAR"){
        return null
    return state

we have to use the initialState as null as at the start no one will be logged in. Exporting the function with parameters state and action. State Define the current state and action perform different tasks and returns the payload. Payload is the data that is stored in states and used all over the Web App.

App.js file configuration

import React, { createContext,useContext,useReducer,useEffect } from 'react';
import {BrowserRouter as Router,Route,Switch} from 'react-router-dom';

import {reducer,initialState} from './reducer/userReducer';

export const UserContext = createContext();

const Routing = () => {

  const {state,dispatch} = useContext(UserContext);
    const user = JSON.parse(localStorage.getItem("user"))


      <Route exact path="/"><Home /></Route>
      <Route exact path="/login"><Login /></Route>

function App() {

  const [state,dispatch] = useReducer(reducer,initialState)

  return (
    <UserContext.Provider value={{state,dispatch}} >
      <Navbar />
      <Routing />
      <Footer />

export default App;

Here we imported the reducer and using the useEffect hook as each time page reload we get the status that is there any user is logged in. And in the App function, we render the pages under the Provider of the given reducer which helps in saving the data to states.

Login Function

In Login, the function collects the inputs using the form and stores them in variables, and then passes them with the specific API or URL. After calling the URL then the returned data we use the dispatch function of the useContext Hook to save the data in states and give access rights to the authenticated user.

import React,{useState,useContext} from 'react';
import {Link,useHistory} from 'react-router-dom'
import axios from 'axios'
import { UserContext } from '../App';

const Login = () => {

    const {state,dispatch} = useContext(UserContext)
    const history  = useHistory()
    const [values,setValues] = useState({
        password: '',

    const {userName,password} = values;

    const handleChange = name => e => {

    const handleSubmit = (e) => {
        if(!userName || !password){
            // M.toast({html:"Empty Fields",classes:"#c62828 red darken-3"})
            // M.toast({html:"Signedin Success",classes:"#43a047 green darken-1"})
        }).catch((err) =>{
            // M.toast({html:"Invalid Credentials",classes:"#c62828 red darken-3"})


    return (
        <br />
            <nav aria-label="breadcrumb">
                <ol className="breadcrumb"  style={{padding:'13px','backgroundColor': '#074235'}}>
                    <li className="breadcrumb-item text-light"><Link to="/">Home</Link></li>
                    <li className="breadcrumb-item text-light active" aria-current="page">Login</li>
        <hr />

        <div className="container">
            <div className="card shadow  mb-5 bg-body rounded" style={{ width: '700px' }}>
                <h5 className="card-header" style={{ 'backgroundColor': '#074235', color: 'white' }}>Login</h5>
                <div className="card-body">
                    <form type="post" className="form-floating">

                        <div className="mb-3">
                            <label htmlFor="exampleInputEmail1" className="form-label">UserName </label>
                            <input required placeholder="Enter username" 
                                className="form-control" />
                            <div className="form-text"><font color="red" style={{ 'fontSize': '13px' }}></font></div>

                        <div className="mb-3">
                            <label htmlFor="exampleInputPassword1" className="form-label">Password</label>
                            <input required type="password" 
                                placeholder="Enter Password" 
                                className="form-control" />
                            <div className="form-text"><font color="red" style={{ 'fontSize': '13px' }}></font></div>

                        <button type="submit" className="btn btn-primary pull-right"
                        onClick={e => handleSubmit(e)}

export default Login

Use the Console of the Browser to check on the state of the web app or see the localStorage under the Application section in chrome developer tools