Skip to content

Instantly share code, notes, and snippets.

@carlqt
Last active June 8, 2017 01:28
Show Gist options
  • Save carlqt/6e4926d250d9e185e9f67a6ce804ca01 to your computer and use it in GitHub Desktop.
Save carlqt/6e4926d250d9e185e9f67a6ce804ca01 to your computer and use it in GitHub Desktop.
AuthComponent wrapper for handling restricted routes in react.
//AuthRoute.js snippet
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
class AuthRoute extends React.Component {
constructor(props) {
super(props);
this.authenticated = this.authenticated.bind(this);
}
authenticated() {
// Check for the token here
// If token does not exists, return false
// If token exists, check if it is also in the redux state
// > if token is not in the redux state, validate token in the server
// > If validation failed, return false else return true and store in redux state
// If token exists in redux state as well, return true
return false;
}
render() {
return(
<div>
{this.authenticated() ? this.props.children : <Redirect to={this.props.back}/> }
</div>
)
}
}
export default AuthRoute;
import { Route, Switch } from 'react-router-dom';
import React from 'react';
import Property from './Property/Property';
import Upload from './Upload/Index';
import Cards from './Cards/Cards';
import AgentSignIn from './Agent/SignIn/SignIn';
import Protected from './Agent/Protected';
import SuperProtected from './Agent/superprotected';
import AuthRoute from './hoc/AuthRoute';
export default () => (
<Switch>
<Route exact path="/" component={Cards} />
<Route exact path="/property/:name" component={Property} />
<Route exact path="/property_upload" component={Upload} />
<Route exact path="/agent/sign_in" component={AgentSignIn} />
{/*<Route exact path="/agent/protected" component={Protected} />*/}
<AuthRoute back={"/agent/sign_in"}>
<Route exact path="/agent/protected" component={Protected} />
<Route exact path="/agent/superprotected" component={SuperProtected} />
</AuthRoute>
</Switch>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment