-
-
Save astrotars/87d3f05545e2d56869237aa9a9d7cca7 to your computer and use it in GitHub Desktop.
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
import React from "react"; | |
import { Redirect, Route } from "react-router-dom"; | |
const AuthedRoute = ({ component: Component, loading, ...rest }) => { | |
const isAuthed = Boolean(localStorage.getItem("token")); | |
return ( | |
<Route | |
{...rest} | |
render={props => | |
loading ? ( | |
<p>Loading...</p> | |
) : !isAuthed ? ( | |
<Component history={props.history} {...rest} /> | |
) : ( | |
<Redirect | |
to={{ | |
pathname: "/" | |
}} | |
/> | |
) | |
} | |
/> | |
); | |
}; | |
export default AuthedRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment