Implement Protected Routes for Authenticated Users with Supabase Auth

Share this video with your friends

Send Tweet

Since Supabase requires our user to be authenticated to fetch tweets, it doesn't make sense for an unauthenticated user to visit the landing page. In this lesson, we implement protected routes that are only accessible to authenticated users.

Additionally, we create a /login route to redirect unauthenticated users, which allows them to sign in with GitHub.

Code Snippets

Redirect unauthenticated users

const {
  data: { session },
} = await supabase.auth.getSession();

if (!session) {
  redirect("/login");
}

Resources