Skip to content
Esc
navigateopen⌘Jpreview
Dashboard

4. Protecting a website route

Protect website routes by requiring user authentication and redirecting unauthenticated users to login.

UI type

Protecting a website route means that it cannot be accessed unless a user is signed in. A signed-out user is redirected to the login page.

Let’s say we want to protect the home page of your website (/ route). In this case, we can edit the /pages/index.tsx file to add an auth wrapper around your Home component like so:

import React from "react";
import { SessionAuth } from "supertokens-auth-react/recipe/session";
import ProtectedPage from "./protectedPage";

export default function Home() {
  return (
    // we protect ProtectedPage by wrapping it with SessionAuth
    <SessionAuth>
      <ProtectedPage />
    </SessionAuth>
  );
}

API reference

API schema and response details