TanStackT
TanStack5mo ago
3 replies
radical-lime

No redirection using throw redirect in middleware

Hello, there is something I don't understand, below is my setup, why the redirect does not work ?? I just get an error on my ErrorCatchBoundary. It is like the error is not handled by the router lifecycle, but I don't understand why....

// My dashboard route
export const Route = createFileRoute("/_admin/admin/_layout/dashboard")({
    loader: async ({ context: { queryClient } }) => {
        return queryClient.ensureQueryData(adminOverviewOptions());
    },
    component: DashboardPage,
});

// The query option with the server function
export const adminOverviewOptions = () => queryOptions({
    queryKey: adminQueryKeys.adminOverviewKey(),
    queryFn: () => getAdminOverview(),
});

// The server function with the middlewares
export const getAdminOverview = createServerFn({ method: "GET" })
    .middleware([managerAuthMiddleware, adminAuthMiddleware])
    .handler(async () => {
        const userService = await getContainer().then((c) => c.services.user);
        return userService.getAdminOverview();
    });

// The admin middleware which check the token, if bad token should redirect to "/admin"
export const adminAuthMiddleware = createMiddleware({ type: "function" }).server(async ({ next }) => {
    const adminToken = getCookie(ADMIN_COOKIE_NAME);

    if (!adminToken || !verifyAdminToken(adminToken)) {
        throw redirect({ to: "/admin" });
    }

    return next();
});


when the throw redirect is hit i just get my "An error occurred page" instead of a redirection. (I'm using SPA mode, defaultSsr to false in router and a shell component for information).

Thanks in advance :).
Was this page helpful?