From f93d1187fd5b1b23c811737c459548341eca64f0 Mon Sep 17 00:00:00 2001 From: Eva Decker Date: Sun, 1 Dec 2024 23:19:07 -0500 Subject: [PATCH] Reconfigure redirect logic for auth users --- src/routes/_authenticated.tsx | 11 +++++------ src/routes/_unauthenticated.tsx | 7 +------ src/routes/_unauthenticated/signin.tsx | 6 +++++- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/routes/_authenticated.tsx b/src/routes/_authenticated.tsx index 6a2d547..3f8ca5a 100644 --- a/src/routes/_authenticated.tsx +++ b/src/routes/_authenticated.tsx @@ -1,15 +1,14 @@ -import { Navigate, Outlet, createFileRoute } from "@tanstack/react-router"; -import { useConvexAuth } from "convex/react"; +import { Outlet, createFileRoute, redirect } from "@tanstack/react-router"; export const Route = createFileRoute("/_authenticated")({ + beforeLoad: async ({ context }) => { + const isAuthenticated = await context.auth; + if (!isAuthenticated) throw redirect({ to: "/signin" }); + }, component: AuthenticatedRoute, }); function AuthenticatedRoute() { - const { isAuthenticated } = useConvexAuth(); - - if (!isAuthenticated) return ; - return (
diff --git a/src/routes/_unauthenticated.tsx b/src/routes/_unauthenticated.tsx index 71e4f12..83edd70 100644 --- a/src/routes/_unauthenticated.tsx +++ b/src/routes/_unauthenticated.tsx @@ -1,14 +1,9 @@ -import { Navigate, Outlet, createFileRoute } from "@tanstack/react-router"; -import { useConvexAuth } from "convex/react"; +import { Outlet, createFileRoute } from "@tanstack/react-router"; export const Route = createFileRoute("/_unauthenticated")({ component: UnauthenticatedRoute, }); function UnauthenticatedRoute() { - const { isAuthenticated } = useConvexAuth(); - - if (isAuthenticated) return ; - return ; } diff --git a/src/routes/_unauthenticated/signin.tsx b/src/routes/_unauthenticated/signin.tsx index 9b3cd6f..9ed0b0f 100644 --- a/src/routes/_unauthenticated/signin.tsx +++ b/src/routes/_unauthenticated/signin.tsx @@ -15,13 +15,17 @@ import { TooltipTrigger, } from "@/components/common"; import { useAuthActions } from "@convex-dev/auth/react"; -import { createFileRoute, useNavigate } from "@tanstack/react-router"; +import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router"; import { ConvexError } from "convex/values"; import { ChevronLeft } from "lucide-react"; import { useState } from "react"; import type { Key } from "react-aria"; export const Route = createFileRoute("/_unauthenticated/signin")({ + beforeLoad: async ({ context }) => { + const isAuthenticated = await context.auth; + if (isAuthenticated) throw redirect({ to: "/" }); + }, component: LoginRoute, });