Create a Route That Redirects
Let’s first create a route that will check if the user is logged in before routing.
Add the following to src/components/AuthenticatedRoute.js.
import React from "react";
import { Route, Redirect, useLocation } from "react-router-dom";
import { useAppContext } from "../lib/contextLib";
export default function AuthenticatedRoute({ children, ...rest }) {
const { pathname, search } = useLocation();
const { isAuthenticated } = useAppContext();
return (
<Route {...rest}>
{isAuthenticated ? (
children
) : (
<Redirect to={
`/login?redirect=${pathname}${search}`
} />
)}
</Route>
);
}
This simple component creates a Route where its children are rendered only if the user is authenticated. If the user is not authenticated, then it redirects to the login page. Let’s take a closer look at it:
-
Like all components in React,
AuthenticatedRoutehas a prop calledchildrenthat represents all child components. Example child components in our case would beNewNote,NotesandSettings. -
The
AuthenticatedRoutecomponent returns a React RouterRoutecomponent. -
We use the
useAppContexthook to check if the user is authenticated. -
If the user is authenticated, then we simply render the
childrencomponent. And if the user is not authenticated, then we use theRedirectReact Router component to redirect the user to the login page. -
We also pass in the current path to the login page (
redirectin the query string). We will use this later to redirect us back after the user logs in. We use theuseLocationReact Router hook to get this info.
We’ll do something similar to ensure that the user is not authenticated.
Add the following to src/components/UnauthenticatedRoute.js.
import React, { cloneElement } from "react";
import { Route, Redirect } from "react-router-dom";
import { useAppContext } from "../lib/contextLib";
export default function UnauthenticatedRoute(props) {
const { children, ...rest } = props;
const { isAuthenticated } = useAppContext();
return (
<Route {...rest}>
{!isAuthenticated ? (
cloneElement(children, props)
) : (
<Redirect to="/" />
)}
</Route>
);
}
Here we are checking to ensure that the user is not authenticated before we render the child components. Example child components here would be Login and Signup. And in the case where the user is authenticated, we use the Redirect component to simply send the user to the homepage.
The cloneElement above makes sure that passed in state is handled correctly for child components of UnauthenticatedRoute routes.
Next, let’s use these components in our app.
For help and discussion
Comments on this chapter