Skip to content

Instantly share code, notes, and snippets.

@vschoener
Last active July 26, 2018 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vschoener/4a9a5cef33de13105aba580990638f70 to your computer and use it in GitHub Desktop.
Save vschoener/4a9a5cef33de13105aba580990638f70 to your computer and use it in GitHub Desktop.
Restrict authenticated routes on React with Typescript
import { Route } from 'react-router-dom';
import { connect } from 'react-redux';
import { IAppState } from '../reducers/index';
import { RouteProps } from 'react-router';
import history from '../config/history';
interface Props extends RouteProps {
authenticated: string | null;
}
class AuthorizedRouteHOC extends Route<Props> {
componentDidMount() {
if (!this.props.authenticated) {
history.push('/login');
}
}
}
function mapStateToProps(state: IAppState) {
return {
authenticated: state.auth.authenticated,
}
}
export default connect(mapStateToProps)(AuthorizedRouteHOC);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment