'use client' import React, { useEffect, useState } from 'react' import { signIn, useSession } from "next-auth/react" import { ReactNode } from "react" import { useQuery } from "@tanstack/react-query"; const queryAuthFn = async () => { const cookie = await fetch("https://beta.api.cocorobo.cn/api/getcookieuserid", { method: "GET", credentials: 'include', }); const cookiejson = await cookie.json(); const user = cookiejson?.[0]?.[0]; if (cookie.ok && user) { return user } return null } const LoadingMask = () => { const { status } = useSession() if (status === 'loading') { return (
) } return null } const AuthModal = ({ refetch }: { refetch: () => void }) => { useEffect(() => { const intervalId = setInterval(() => { refetch() }, 5000) return () => clearInterval(intervalId) }, [refetch]) return (
) } export function AuthProvider({ children }: { children: ReactNode }) { const { status } = useSession() const { data: user, refetch, isLoading, isError, isFetching } = useQuery({ queryKey: ['auth'], queryFn: queryAuthFn }) useEffect(() => { if (user && !isLoading && !isError && !isFetching) { signIn('credentials', { redirect: false, userId: user.userid }) } }, [user, isLoading, isError, isFetching]) if (status === 'authenticated' && !isLoading && !isError && !isFetching) { return children } return ( ) }