connected api with profile, order

This commit is contained in:
Jelaletdin12
2025-11-15 16:14:01 +05:00
parent 21b9e88c5c
commit f867896817
70 changed files with 2370 additions and 2317 deletions

View File

@@ -1,72 +1,71 @@
"use client"
"use client";
import { useEffect, type ReactNode } from "react"
import { useRouter, usePathname } from "next/navigation"
import { useAuthStatus, useGetGuestToken } from "@/lib/hooks/useAuth"
import { useEffect, type ReactNode } from "react";
import { useRouter, usePathname } from "next/navigation";
import { useAuthStatus, useGetGuestToken } from "@/lib/hooks/useAuth";
import { useUserProfile } from "@/features/profile/hooks/useUserProfile";
interface AuthWrapperProps {
children: ReactNode
requireAuth?: boolean
redirectTo?: string
locale: string
children: ReactNode;
requireAuth?: boolean;
redirectTo?: string;
locale: string;
}
/**
* AuthWrapper - Protects routes and manages authentication state
*
* Usage:
* - Wrap protected pages: <AuthWrapper requireAuth>{content}</AuthWrapper>
* - Wrap public pages: <AuthWrapper>{content}</AuthWrapper>
*/
export default function AuthWrapper({
children,
requireAuth = false,
redirectTo,
locale,
}: AuthWrapperProps) {
const router = useRouter()
const pathname = usePathname()
const { isAuthenticated, isLoading, user } = useAuthStatus()
const { mutate: getGuestToken, isPending: isGettingGuestToken } = useGetGuestToken()
const router = useRouter();
const pathname = usePathname();
const { isAuthenticated, isLoading } = useAuthStatus();
const { mutate: getGuestToken, isPending: isGettingGuestToken } = useGetGuestToken();
// Login olmuş kullanıcı için profil bilgisini otomatik çek
useUserProfile();
useEffect(() => {
// Wait for auth check to complete
if (isLoading || isGettingGuestToken) return
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) {
getGuestToken();
}
}, [isLoading, getGuestToken, isGettingGuestToken]);
useEffect(() => {
if (isLoading || isGettingGuestToken) return;
// If auth required and user not authenticated
if (requireAuth && !isAuthenticated) {
const redirect = redirectTo || `/${locale}/login`
const returnUrl = pathname !== redirect ? `?returnUrl=${encodeURIComponent(pathname)}` : ""
router.push(`${redirect}${returnUrl}`)
return
const redirect = redirectTo || `/${locale}/login`;
const returnUrl = pathname !== redirect ? `?returnUrl=${encodeURIComponent(pathname)}` : "";
router.push(`${redirect}${returnUrl}`);
return;
}
// If already authenticated and trying to access login/register
if (isAuthenticated && (pathname.includes("/login") || pathname.includes("/register"))) {
router.push(`/${locale}`)
return
router.push(`/${locale}`);
}
}, [isAuthenticated, isLoading, requireAuth, pathname, router, locale, redirectTo, isGettingGuestToken]);
// Ensure guest token exists for non-authenticated users
if (!isAuthenticated && !requireAuth) {
const hasGuestToken = document.cookie
.split("; ")
.some((row) => row.startsWith("guestToken="))
if (!hasGuestToken) {
getGuestToken()
}
}
}, [isAuthenticated, isLoading, requireAuth, pathname, router, locale, redirectTo, getGuestToken, isGettingGuestToken])
// Show loading state
if (isLoading || (requireAuth && !isAuthenticated)) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-gray-900" />
<div className="flex flex-col items-center gap-4">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600" />
<p className="text-sm text-gray-600">Yükleniyor...</p>
</div>
</div>
)
);
}
return <>{children}</>
return <>{children}</>;
}