Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created September 2, 2020 19:17
Show Gist options
  • Save astrotars/c4f465f8d26ce7206ae72d5894851972 to your computer and use it in GitHub Desktop.
Save astrotars/c4f465f8d26ce7206ae72d5894851972 to your computer and use it in GitHub Desktop.
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: "/auth/login",
state: { next: props.location }
}}
/>
)
}
/>
);
};
export default AuthedRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment