New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-secure-route

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-secure-route

React router package which provide private, public and protected routes with restricted functionality. which helps to authenticate different routes in react App.

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

react-secure-route

A lightweight secure route components on top of react-router-dom.

It have private, public and protected route, which will give you facility to handle you restricted and authenticated route. These component also gives you route props.

Version Downloads/week License Forks on GitHub Forks on GitHub minified + gzip size

Why?

  • Bundle size
  • No dependencies
  • Light weighted
  • Seperate route for public, authenticated and restricated.

Quick Start

Demo

Install

npm install react-secure-route # yarn add react-secure-route

Usage

import React, { useState } from "react";
import { BrowserRouter as Router, Switch, Link } from "react-router-dom";
import { Home, Login, About } from "./pages";
import { PrivateRoute, ProtectedRoute, PublicRoute } from "react-secure-route";

const App = () => {
  const [isLoggedIn, setIsLoggedIn] = useState(
    !!localStorage.getItem("isLoggedIn")
  );

  const renderHeader = () => {
    if (isLoggedIn) {
      return (
        <div>
          <Link to="/">Home</Link>
          <Link to="/about">About</Link>
          <button
            onClick={() => {
              localStorage.removeItem("isLoggedIn");
              setIsLoggedIn(false);
            }}
          >
            Logout
          </button>
        </div>
      );
    }
    return (
      <div>
        <Link to="/">Home</Link>
        <Link to="/about">About</Link>
        <button
          onClick={() => {
            localStorage.setItem("isLoggedIn", true);
            setIsLoggedIn(true);
          }}
        >
          Login
        </button>
      </div>
    );
  };

  return (
    <Router>
      {renderHeader()}
      <Switch>
        <PrivateRoute
          component={Home}
          exact
          isAuthenticated={isLoggedIn}
          redirect="/login"
          path="/"
        />
        <ProtectedRoute
          component={Login}
          restricted={isLoggedIn}
          exact
          redirect="/"
          path="/login"
        />
        <PublicRoute component={About} path="/about" exact />
        /**
         * You can also pass your component like this. You will not get the route props on you component.
          <PrivateRoute
            exact
            isAuthenticated={isLoggedIn}
            redirect="/login"
            path="/"
          >
            <Home />
          </PrivateRoute>
        */
      </Switch>
    </Router>
  );
};

export default App;

Note:- To work this module properly, kindly install react, react-router-dom, and prop-types.

PrivateRoute

This component handles authentication based on the passed props.

Props

PropRequiredTypeDefault ValueDescription
isAuthenticatedNoboolfalseif this is true and restricted props is false your actual component will render else redirect it to passed redirect prop route.
componentNoReact Component_if component is passed inside component prop, you will get the route props on your component.
childrenNoReact Component_if component is passed as a chidren, you will not get the route props on your component.
redirectNostring/loginwhen authentication fails, it will redirect to the given path with the state from it got redirected.
restrictedNoboolfalseif this is true it will redirect to passed redirect prop route.
restNo__you can pass the route props of react-router-dom as well.

ProtectedRoute

This component handles only restricated route based on the passed props.

Props

PropRequiredTypeDefault ValueDescription
componentNoReact Component_if component is passed inside component prop, you will get the route props on your component.
childrenNoReact Component_if component is passed as a chidren, you will not get the route props on your component.
redirectNostring/when authentication fails, it will redirect to the given path with the state from it got redirected.
restrictedNoboolfalseif this is true it will redirect to passed redirect prop route.
restNo__you can pass the route props of react-router-dom as well.

PublicRoute

This component is same as Route component of react-router-dom.

Props

PropRequiredTypeDefault ValueDescription
componentNoReact Component_if component is passed inside component prop, you will get the route props on your component.
childrenNoReact Component_if component is passed as a chidren, you will not get the route props on your component.
restNo__you can pass the route props of react-router-dom as well.

Keywords

react

FAQs

Package last updated on 17 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts