Skip to content

Instantly share code, notes, and snippets.

@bnhansn
Created October 21, 2016 15:37
Show Gist options
  • Save bnhansn/5519eadacc3c0c6bfb62069b82c916e1 to your computer and use it in GitHub Desktop.
Save bnhansn/5519eadacc3c0c6bfb62069b82c916e1 to your computer and use it in GitHub Desktop.
// @flow
import React from 'react';
import { Match, Redirect } from 'react-router';
type Props = {
component: any,
pattern: string,
exactly?: boolean,
isAuthenticated: boolean,
willAuthenticate: boolean,
}
const RedirectAuthenticated = ({
pattern,
exactly,
isAuthenticated,
willAuthenticate,
component: Component,
}: Props) =>
<Match
exactly={exactly}
pattern={pattern}
render={(props) => {
if (isAuthenticated) { return <Redirect to={{ pathname: '/' }} />; }
if (willAuthenticate) { return null; }
if (!willAuthenticate && !isAuthenticated) { return <Component {...props} />; }
return null;
}}
/>;
export default RedirectAuthenticated;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment