npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-secure-route

v0.0.2

Published

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

Downloads

18

Readme

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

| Prop | Required | Type | Default Value | Description | | ----------------- | -------- | ---- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | | isAuthenticated | No | bool | false | if this is true and restricted props is false your actual component will render else redirect it to passed redirect prop route. | | component | No | React Component | _ | if component is passed inside component prop, you will get the route props on your component.| | children | No | React Component | _ | if component is passed as a chidren, you will not get the route props on your component. |
| redirect | No | string | /login | when authentication fails, it will redirect to the given path with the state from it got redirected. | | restricted | No | bool | false | if this is true it will redirect to passed redirect prop route. | | rest | No | _ | _ | 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

| Prop | Required | Type | Default Value | Description | | ----------------- | -------- | ---- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | | component | No | React Component | _ | if component is passed inside component prop, you will get the route props on your component.| | children | No | React Component | _ | if component is passed as a chidren, you will not get the route props on your component. |
| redirect | No | string | / | when authentication fails, it will redirect to the given path with the state from it got redirected. | | restricted | No | bool | false | if this is true it will redirect to passed redirect prop route. | | rest | No | _ | _ | 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

| Prop | Required | Type | Default Value | Description | | ----------------- | -------- | ---- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | | component | No | React Component | _ | if component is passed inside component prop, you will get the route props on your component.| | children | No | React Component | _ | if component is passed as a chidren, you will not get the route props on your component. |
| rest | No | _ | _ | you can pass the route props of react-router-dom as well. |