Skip to content

Private Route with React Router

RequireAuth can only be used with React Router v6. If you are using React Router v5, please use React-kuth-kit v1.x with PrivateRoute.

Installation

To use Private Route with React Router, you need to install the react-router plugin for react auth kit.

To install and save in your package.json dependencies, run:

Install With NPM
npm install --save @auth-kit/react-router
Install With Yarn
yarn add @auth-kit/react-router
Install With PNPM
pnpm install --save @auth-kit/react-router

Usage

@auth-kit/react-router comes with 2 useful functions that help to manage routing with React Router.

These Functions are:

  1. AuthOutlet - Used to Secure multiple routes at once.
  2. RequireAuth - Used to Secure individual routes one at a time, preferred for a granular approach.

AuthOutlet

Import

Import AuthOutlet
import AuthOutlet from '@auth-kit/react-router/AuthOutlet'

Usage

Add AuthOutlet in your Routes Files inside BrowserRouter or HashRouter

RouteComponent.jsx
<BrowserRouter>
  <Routes>
    <Route path={'/'} element={<Home/>}/>
    <Route path={'/login' } element={<Login/>}/>
    <Route element={<AuthOutlet fallbackPath='/login' />}>
      <Route path='/' element={<Users/>} />
      <Route path='/products' element={<Products/>} />
    </Route>
  </Routes>
</BrowserRouter>
Full Code
import React from "react";
import {BrowserRouter, Routes, Route} from "react-router-dom";
import AuthOutlet from '@auth-kit/react-router/AuthOutlet';
import SecureComponent from "./SecureComponent";

const Routes = () => {
  return (
    <BrowserRouter>
      <Routes>
        <Route path={'/'} element={<Home/>}/>
        <Route path={'/login' } element={<Login/>}/>
        <Route element={<AuthOutlet fallbackPath='/login' />}>
          <Route path='/' element={<Users/>} />
          <Route path='/products' element={<Products/>} />
        </Route>
      </Routes>
    </BrowserRouter>
  )
}

RequireAuth

Import

Import RequireAuth
import RequireAuth from '@auth-kit/react-router/RequireAuth'

Usage

Add RequireAuth in your Routes Files inside BrowserRouter or HashRouter

RouteComponent.jsx
<BrowserRouter>
  <Routes>
    <Route path={'/'} element={<Home/>}/>
    <Route path={'/login' } element={<Login/>}/>
    <Route path={'/secure'} element={
      <RequireAuth fallbackPath={'/login'}>
        <SecureComponent/>
          </RequireAuth>
      }
    />
  </Routes>
</BrowserRouter>
Full Code
import React from "react";
import {BrowserRouter, Routes, Route} from "react-router-dom";
import RequireAuth from '@auth-kit/react-router/RequireAuth';
import SecureComponent from "./SecureComponent";

const Routes = () => {
  return (
    <BrowserRouter>
      <Routes>
        <Route path={'/'} element={<Home/>}/>
        <Route path={'/login' } element={<Login/>}/>
        <Route path={'/secure'} element={
          <RequireAuth fallbackPath={'/login'}>
            <SecureComponent/>
          </RequireAuth>
        }
        />
      </Routes>
    </BrowserRouter>
  )
}

— 🔑 —

React Auth Kit is Apache 2.0 License code