fixed some bugs

This commit is contained in:
Jelaletdin12
2025-12-18 23:19:45 +05:00
parent 6d0064b106
commit 0fb4e2765c
36 changed files with 1430 additions and 1485 deletions

View File

@@ -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}</>;