fixed some bugs
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// components/AuthWrapper.tsx
|
||||
|
||||
"use client";
|
||||
|
||||
import { useEffect, type ReactNode } from "react";
|
||||
@@ -5,6 +7,7 @@ import { useRouter, usePathname } from "next/navigation";
|
||||
import { useAuthStatus, useGetGuestToken } from "@/lib/hooks/useAuth";
|
||||
import { useUserProfile } from "@/features/profile/hooks/useUserProfile";
|
||||
import Preloader from "@/components/PageLoader/PreLoader";
|
||||
import TokenStorage from "@/lib/tokenStorage";
|
||||
|
||||
interface AuthWrapperProps {
|
||||
children: ReactNode;
|
||||
@@ -24,27 +27,23 @@ export default function AuthWrapper({
|
||||
const { isAuthenticated, isLoading } = useAuthStatus();
|
||||
const { mutate: getGuestToken, isPending: isGettingGuestToken } = useGetGuestToken();
|
||||
|
||||
// Login olmuş kullanıcı için profil bilgisini otomatik çek
|
||||
// Fetch user profile only if authenticated
|
||||
useUserProfile();
|
||||
|
||||
// Initialize guest token if needed
|
||||
useEffect(() => {
|
||||
if (isLoading) return;
|
||||
|
||||
const authToken = document.cookie
|
||||
.split("; ")
|
||||
.find(row => row.startsWith("authToken="));
|
||||
const guestToken = document.cookie
|
||||
.split("; ")
|
||||
.find(row => row.startsWith("guestToken="));
|
||||
|
||||
if (!authToken && !guestToken && !isGettingGuestToken) {
|
||||
if (!TokenStorage.hasAnyToken() && !isGettingGuestToken) {
|
||||
getGuestToken();
|
||||
}
|
||||
}, [isLoading, getGuestToken, isGettingGuestToken]);
|
||||
|
||||
// Handle redirects
|
||||
useEffect(() => {
|
||||
if (isLoading || isGettingGuestToken) return;
|
||||
|
||||
// Redirect to login if auth required but not authenticated
|
||||
if (requireAuth && !isAuthenticated) {
|
||||
const redirect = redirectTo || `/${locale}/login`;
|
||||
const returnUrl = pathname !== redirect ? `?returnUrl=${encodeURIComponent(pathname)}` : "";
|
||||
@@ -58,9 +57,7 @@ export default function AuthWrapper({
|
||||
}, [isAuthenticated, isLoading, requireAuth, pathname, router, locale, redirectTo, isGettingGuestToken]);
|
||||
|
||||
if (isLoading || (requireAuth && !isAuthenticated)) {
|
||||
return (
|
||||
<Preloader/>
|
||||
);
|
||||
return <Preloader />;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
|
||||
Reference in New Issue
Block a user