Forked from VictorCoding/gist:dd3996a4f29aad694af45f0924f4e349
Last active
October 8, 2016 02:28
-
-
Save tkh44/9b3d56c55038926776a23f4f2302f824 to your computer and use it in GitHub Desktop.
auth comonent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MatchWhenAuthorized extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
authInProgress: true, | |
isAuthenticated: false, | |
} | |
} | |
componentWillMount = () { | |
this.setState({ authInProgress: true }); | |
Firebase.auth().onAuthStateChanged((user) => { | |
this.setState({ | |
authInProgress: false | |
isAuthenticated: user ? true : false | |
}); | |
}); | |
} | |
render() { | |
const { authInProgress, isAuthenticated } = this.state; | |
const { component: Component, ...rest } = this.props; | |
// You have a couple of options here | |
// You can render null while in progress (boo) | |
/* | |
if (authInProgress) { | |
return null | |
} | |
*/ | |
return ( | |
<Match | |
{ ...rest } | |
children={({ matched, ...props }) => { | |
if (!matched) { | |
return null; | |
} | |
if (authInProgress) { | |
return (<div>Logging In (PRETEND THERE IS A SPINNER)</div>); | |
} | |
if (isAuthenticated) { | |
return (<Component {...props}/>); | |
} | |
return (<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