react-guarded-route

react-guarded-route allows you to guard your routes by a validation fuction.

Usage no npm install needed!

<script type="module">
  import reactGuardedRoute from 'https://cdn.skypack.dev/react-guarded-route';
</script>

README

react-guarded-route

NPM JavaScript Style Guide

react-guarded-route allows you to guard your routes by a validation fuction.

Install

npm install --save react-guarded-route

Usage

import React, { Component } from 'react'

import GuardedRoute from 'react-guarded-route'
import { BrowserRouter, Route, Switch } from "react-router-dom";
import Dashboard from './Dashboard';
import Home from './Home';

const validatorFunction = () => {
  return true; //your validation logic
}

export default function App() {
  return(
    <Router>
        <Switch>
            <Route exact path = '/' component={Home}></Route>
            <GuardedRoute 
                path = '/dashboard'
                component = {Dashboard}
                redirectTo = '/login' //(Optional) Redirect to '/login' if validatorFunction returns false. Will redirect to '/' if not provided. 
                validatorFunction = {validatorFunction()}>
        </Switch>
    </Router>
  )
}

License

MIT ©