Skip to content

Commit

Permalink
fix: Reconfigure redirect logic for auth users (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
evadecker authored Dec 3, 2024
1 parent 335f39c commit 9bcdce2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-ducks-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"namesake": patch
---

Fix redirect logic for authenticated and unauthenticated users
20 changes: 11 additions & 9 deletions src/routes/_authenticated.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Navigate, Outlet, createFileRoute } from "@tanstack/react-router";
import { useConvexAuth } from "convex/react";
import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
import { Authenticated } from "convex/react";

export const Route = createFileRoute("/_authenticated")({
beforeLoad: async ({ context }) => {
const { isAuthenticated, isLoading } = await context.auth;
if (!isLoading && !isAuthenticated) throw redirect({ to: "/signin" });
},
component: AuthenticatedRoute,
});

function AuthenticatedRoute() {
const { isAuthenticated } = useConvexAuth();

if (!isAuthenticated) return <Navigate to="/signin" />;

return (
<main className="text-gray-normal">
<Outlet />
</main>
<Authenticated>
<main className="text-gray-normal">
<Outlet />
</main>
</Authenticated>
);
}
17 changes: 5 additions & 12 deletions src/routes/_authenticated/_home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
type Status,
} from "@convex/constants";
import { Outlet, createFileRoute } from "@tanstack/react-router";
import { Authenticated, Unauthenticated, useQuery } from "convex/react";
import { useQuery } from "convex/react";
import {
History,
List,
Expand Down Expand Up @@ -219,16 +219,9 @@ function IndexRoute() {
};

return (
<>
<Authenticated>
<Container className="flex">
<MyQuests />
<Outlet />
</Container>
</Authenticated>
<Unauthenticated>
<h1>Please log in</h1>
</Unauthenticated>
</>
<Container className="flex">
<MyQuests />
<Outlet />
</Container>
);
}
7 changes: 1 addition & 6 deletions src/routes/_unauthenticated.tsx
Original file line number Diff line number Diff line change
@@ -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 <Navigate to="/" />;

return <Outlet />;
}
6 changes: 5 additions & 1 deletion src/routes/_unauthenticated/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, isLoading } = await context.auth;
if (!isLoading && isAuthenticated) throw redirect({ to: "/" });
},
component: LoginRoute,
});

Expand Down

0 comments on commit 9bcdce2

Please sign in to comment.