Is this correct way to do session error handling in tanstack router (React)

export const Route = createFileRoute("/_authenticated")({
loader: async () => {
const isAuthenticated =
window.localStorage.getItem("isAuthenticated") === "true";

if (!isAuthenticated) {
await signOut({
fetchOptions: {
onSuccess: () => {
throw redirect({
to: "/get-started", // redirect to login page
});
},
},
});
}

const { data: sessionData, error } = await authClient.getSession();

if ((!sessionData && !error) || !isAuthenticated) {
window.localStorage.removeItem("isAuthenticated");

throw redirect({
to: "/get-started", // redirect to login page
});
}

return { sessionData, error };
},
staleTime: 10_000,
component: () => <AuthComponent />,
});
export const Route = createFileRoute("/_authenticated")({
loader: async () => {
const isAuthenticated =
window.localStorage.getItem("isAuthenticated") === "true";

if (!isAuthenticated) {
await signOut({
fetchOptions: {
onSuccess: () => {
throw redirect({
to: "/get-started", // redirect to login page
});
},
},
});
}

const { data: sessionData, error } = await authClient.getSession();

if ((!sessionData && !error) || !isAuthenticated) {
window.localStorage.removeItem("isAuthenticated");

throw redirect({
to: "/get-started", // redirect to login page
});
}

return { sessionData, error };
},
staleTime: 10_000,
component: () => <AuthComponent />,
});
2 Replies
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Anuj
AnujOP7mo ago
This is a little confusion and extra step for better auth @bekacru Could you please check this?

Did you find this page helpful?