1.1.1 • Published 3 years ago

react-private-public-route v1.1.1

Weekly downloads
17
License
MIT
Repository
github
Last release
3 years ago

React-Routing

react-private-public-route npm version styled with prettier

Public and Private Routng with restricted functionality in React

Working demo available at

Github pages

Code Example available at: Github

Installation

Using npm:

$ npm i react-private-public-route

Basic Usage

...
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
...
import { PublicRoute, PrivateRoute } from "react-private-public-route";
import Homepage from './Homepage';
import Restricted from './Restricted';
import Login from './Login';
...

function App() {
    return (
     ...
        <Router>
            <Switch>
              <PublicRoute exact path="/login" component={Login} />
                <PublicRoute
                exact
                restricted
                redirect="/homepage"
                path="/restricted"
                component={Restricted}
                />
                <PrivateRoute
                isAuthenticated={false}
                restricted
                redirect="/login"
                path="/homepage"
                component={Homepage}
                />
            </Switch>
        </Router>
     ...
    )
}

export default App

Public Route

Propsvalue TypeDefaut ValueisRequiredDescription
restrictedboolfalse-If set to true it will be redirected
componentfuntion-yesComponent which needs to be rendered if not restricted
redirectstring/-If restricted it will be redirected to this route

Private Route

Propsvalue TypeDefaut ValueisRequiredDescription
restrictedboolfalse-If set to true it will be redirected
componentfuntion-yesComponent which needs to be rendered if not Restricted and Authenticated
redirectstring/login-If restricted on not authenticated it will be redirected to this route
isAuthenticatedboolfalse-To check whether route is Authenticated

Issues

If you find a bug, please file an issue on our issue tracker on GitHub.

Credits

React Private Public Route is built and maintained by @ronaksonigara.