Skip to content

Instantly share code, notes, and snippets.

@bnhansn
Created October 21, 2016 15:36
Show Gist options
  • Save bnhansn/add8b2c885c4c5f96e89cd6e250d8218 to your computer and use it in GitHub Desktop.
Save bnhansn/add8b2c885c4c5f96e89cd6e250d8218 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 MatchAuthenticated = ({
pattern,
exactly,
isAuthenticated,
willAuthenticate,
component: Component,
}: Props) =>
<Match
exactly={exactly}
pattern={pattern}
render={(props) => {
if (isAuthenticated) { return <Component {...props} />; }
if (willAuthenticate) { return null; }
if (!willAuthenticate && !isAuthenticated) { return <Redirect to={{ pathname: '/login' }} />; }
return null;
}}
/>;
export default MatchAuthenticated;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment