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

protected-react-routes-generator

v1.0.4

Published

React protected routes generator

Downloads

9

Readme

Description

A React utility that is made to:

  1. Handle the protected routes Logic with fully control over rendered route.
  2. ensure that is the only allowed persona can access his specified routes.
  3. Handle redirection
    1. prevent login user to access route that not require authentication and viceversa.

You can play with Demo from here

Example

import React from 'react';
import { Switch } from 'react-router-dom';
import { ConnectedRouter } from 'connected-react-router';
// Utility
import RoutesConfiguration from 'protected-react-routes-generator';
// Routes
import CoursePayment from '../Payment/Course';
import PaymentResult from '../Payment/PaymentResult';
import Login from '../Screens/Login';
import SignUp from '../Screens/SignUp';
import Home from '../components/pages/Home';
import Faqs from '../components/pages/Faqs';
import PageNotFound from '../components/pages/pageNotFound';

function AppNavigation({ auth }) {
    const authorizedStructure = {
    fallbackPath: '/login',
    routes: [
      { path: '/payment/result', component: <PaymentResult /> },
      { path: '/course-payment/:id', component: <CoursePayment /> },
    ]
  };

  const unAuthorizedStructure = {
    fallbackPath: '/dashboard',
    routes: [
      { path: '/signup', component: <SignUp /> },
      { path: '/login/:resetAvailable?', component: <Login /> },
    ]
  };

  const anonymousStructure = {
    routes: [
      { path: '/', component: <Home /> },
      { path: '/home', component: <Home /> },
      { path: '/faqs', component: <Faqs /> },
      { path: '/404', component: <PageNotFound /> },
    ]
  };

  return(
    <ConnectedRouter history={history}>
      <Switch>
        {RoutesConfiguration({
          isAuthenticated: auth.isAuthenticated,
          anonymousStructure,
          authorizedStructure,
          unAuthorizedStructure,
          fallbackComponent: <PageNotFound />
        })}
      </Switch>
    </ConnectedRouter>
  );
}

const mapStateToProps = store => ({
  auth: store.auth
});

export default connect(mapStateToProps)(AppNavigation);

Explanation

The important thing is RoutesConfiguration function which accepts an object that has 5 props

  1. isAuthenticated ----> Boolean
  2. anonymousStructure ----> Object
  3. authorizedStructure ----> Object
  4. unAuthorizedStructure ----> Object
  5. fallbackComponent ----> JSX Element

isAuthenticated

it's [ very important ] because it's used to differentiate LoggedIn/out users to give back the wanted routes.

prop | Type | isRequired ----------------|-------|----------- isAuthenticated | bool | true

anonymousStructure

it's an object that [ ONLY ] has 1 prop which is routes array [ each item is a route Model ], these routes are allowed for all personas regardless he/she authenticated or not.

prop | sibling | Type | isRequired -------------------|---------------------------|---------|------------ anonymousStructure | routes | object | false routes | array of Route Model | array | true

authorizedStructure

it's an object that [ ONLY ] has 2 props which is routes array [ each item is a route Model ], these routes are allowed for [ LoggedIn ] users and the second option is fallbackPath: which is used to redirect to predefined url [ if a user didn't make a login then he tried to access a page from authorized routes ]. prop | sibling | Type | isRequired --------------------|----------------------------|---------|---------------------------------------- authorizedStructure | routes, fallbackPath | object | false routes | array of Route Model | array | true fallbackPath | none | string | true [ in case of routes is provided ]

unAuthorizedStructure

it's an object that [ ONLY ] has 2 props which is routes array [ each item is a route Model ], these routes are allowed for [ LoggedOut ] users and the second option is fallbackPath: which is used to redirect to predefined url [ if a loggedIn user didn't make a logout then he tried to access a page from authorized routes ].

prop | sibling | Type | isRequired ----------------------|----------------------------|---------|---------------------------------------- unAuthorizedStructure | routes, fallbackPath | object | false routes | array of Route Model | array | true fallbackPath | none | string | true [ in case of routes is provided ]

fallbackComponent

it's a jsx component that is used as a fallback for the whole router Like ('404', NotFound)

prop | Type | isRequired ------------------|-------------|----------- fallbackComponent | JSX Element | false

route Model

prop | Type | Usage | isRequired -------------|-----------------|--------------------------------------------------------------------------|------------ path | string | To specify the route path | true component | React.Component | Rendered route component | true routeProps | object | To override route props | false redirectPath | string | To redirect to specific location [ instead of parent fallbackPath ] | false showRouteIf | boolean | To decide when to show/hide the route | false

  const unAuthorizedStructure = {
    fallbackPath: '/home',
    routes: [
      {/*  Route Model */
        /**
         * path: string
         * component: React.Component
         * routeProps: Object -----> To override route props
         * redirectPath: String ----> To redirect to specific location [ instead of fallbackPath ]
         * showRouteIf: Boolean ----> To decide when to show/hide the route
         */
      }
    ]
  }