Skip to content

Instantly share code, notes, and snippets.

@ishan-marikar
Forked from fdidron/App.js
Created June 18, 2018 10:00
Show Gist options
  • Select an option

  • Save ishan-marikar/433ec1f5dbbc1bbd5a617d6993086c8f to your computer and use it in GitHub Desktop.

Select an option

Save ishan-marikar/433ec1f5dbbc1bbd5a617d6993086c8f to your computer and use it in GitHub Desktop.

Revisions

  1. @fdidron fdidron revised this gist Aug 3, 2017. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions AuthRoute.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    import React from 'react';
    import PropTypes from 'prop-types';
    import { Redirect, Route } from 'react-router-dom';

    //Mock of an Auth method, can be replaced with an async call to the backend. Must return true or false
    @@ -34,9 +35,9 @@ const AuthRoute = ({component, ...props}) => {
    };

    AuthRoute.propTypes = {
    component: React.PropTypes.oneOfType([
    React.PropTypes.element,
    React.PropTypes.func
    component: PropTypes.oneOfType([
    PropTypes.element,
    PropTypes.func
    ])
    };

  2. @fdidron fdidron revised this gist Jan 31, 2017. No changes.
  3. @fdidron fdidron revised this gist Jan 31, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions AuthRoute.js
    Original file line number Diff line number Diff line change
    @@ -10,18 +10,24 @@ const PUBLIC_ROOT = '/login';
    const AuthRoute = ({component, ...props}) => {
    const { isPrivate } = component;
    if (isAuthenticated()) {
    //User is Authenticated
    if (isPrivate === true) {
    //If the route is private the user may proceed.
    return <Route { ...props } component={ component } />;
    }
    else {
    //If the route is public, the user is redirected to the app's private root.
    return <Redirect to={ PRIVATE_ROOT } />;
    }
    }
    else {
    //User is not Authenticated
    if (isPrivate === true) {
    //If the route is private the user is redirected to the app's public root.
    return <Redirect to={ PUBLIC_ROOT } />;
    }
    else {
    //If the route is public, the user may proceed.
    return <Route { ...props } component={ component } />;
    }
    }
  4. @fdidron fdidron revised this gist Jan 31, 2017. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions AuthRoute.js
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,4 @@ AuthRoute.propTypes = {
    ])
    };

    export default AuthRoute;

    export default AuthRoute;
  5. @fdidron fdidron created this gist Jan 31, 2017.
    15 changes: 15 additions & 0 deletions App.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    //Usage
    import React from 'react';
    import { BrowserRouter as Router } from 'react-router-dom';
    import Route from './AuthRoute';

    import Login from './Login';
    import Private from './Private';

    export default () =>
    <Router>
    <div>
    <Route component={ Login } path="/login" />
    <Route component={ Private } path="/private" />
    </div>
    </Router>;
    39 changes: 39 additions & 0 deletions AuthRoute.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    import React from 'react';
    import { Redirect, Route } from 'react-router-dom';

    //Mock of an Auth method, can be replaced with an async call to the backend. Must return true or false
    const isAuthenticated = () => true;

    const PRIVATE_ROOT = '/private';
    const PUBLIC_ROOT = '/login';

    const AuthRoute = ({component, ...props}) => {
    const { isPrivate } = component;
    if (isAuthenticated()) {
    if (isPrivate === true) {
    return <Route { ...props } component={ component } />;
    }
    else {
    return <Redirect to={ PRIVATE_ROOT } />;
    }
    }
    else {
    if (isPrivate === true) {
    return <Redirect to={ PUBLIC_ROOT } />;
    }
    else {
    return <Route { ...props } component={ component } />;
    }
    }
    };

    AuthRoute.propTypes = {
    component: React.PropTypes.oneOfType([
    React.PropTypes.element,
    React.PropTypes.func
    ])
    };

    export default AuthRoute;

    export default AuthRoute;
    10 changes: 10 additions & 0 deletions Login.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    import React from 'react';

    export default class Login extends React.Component {

    static isPrivate = false

    render() {
    return <h1>{' Login '}</h1>;
    }
    }
    10 changes: 10 additions & 0 deletions Private.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    import React from 'react';

    export default class Private extends React.Component {

    static isPrivate = true

    render() {
    return <h1>{' Private '}</h1>;
    }
    }