Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auth redirecting even when logged in #90

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions frontend/app/(authenticated)/template.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { Suspense } from "react";
import { Suspense, useEffect } from "react";
import { useRouter } from "next/navigation";
import { useQuery } from "@tanstack/react-query";

Expand All @@ -10,19 +10,37 @@ export default function RedirectIfNotAuthenticated({
children,
}: Readonly<{ children: React.ReactNode }>) {
const router = useRouter();
const { fetchStatus } = useQuery(getUserProfile());

const isLoggedIn = useUserStore((state) => state.isLoggedIn);
const user = useUserStore((state) => state.user);
const { isLoggedIn, setLoggedIn, setNotLoggedIn } = useUserStore(
(state) => state,
);
const {
data: userProfile,
isSuccess: isUserProfileSuccess,
isFetching,
} = useQuery(getUserProfile());

if (fetchStatus !== "fetching") {
if (!isLoggedIn) {
router.push("/login");
useEffect(() => {
console.log({ isUserProfileSuccess, userProfile });
if (!isFetching) {
if (!userProfile) {
if (!isLoggedIn) {
router.push("/login");
}
if (isLoggedIn && !userProfile!.categories.length) {
router.push("/onboarding");
}
}
}
if (isLoggedIn && !user!.categories.length) {
router.push("/onboarding");
}
}
}, [
userProfile,
isUserProfileSuccess,
setLoggedIn,
setNotLoggedIn,
isLoggedIn,
router,
isFetching,
]);

return <Suspense>{children}</Suspense>;
}
Loading