Skip to content

Instantly share code, notes, and snippets.

@Southclaws
Created November 5, 2017 20:16
Show Gist options
  • Save Southclaws/839cb086a54f2cbbedb88556195c8ed3 to your computer and use it in GitHub Desktop.
Save Southclaws/839cb086a54f2cbbedb88556195c8ed3 to your computer and use it in GitHub Desktop.
I wrestled with this for a while so I figured I'd share it.
class App extends React.Component<{}, AppState> {
// IfLoggedIn is a TypeScript version of:
// https://reacttraining.com/react-router/web/example/auth-workflow
// it's just a stateless component that takes path and component (similar to <Route>)
// and renders the component if the user is logged in, if not, it redirects to /login
IfLoggedIn = (thisProps: { path: string; component: Function }) => {
return (
<Route
render={props =>
this.state.loggedIn ? (
thisProps.component
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)}
/>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment