export const userState = atom<string | undefined>({
key: 'user',
default: undefined,
});
export const useAuthFetch = (): IAuthFetch => {
const [user, setUser] = useRecoilState(userState);
const authService = useRecoilValue(authApi);
const { isLoading } = useQuery(queryKeys.auth.me(), () => authService.me(), {
retry: false,
refetchOnWindowFocus: false,
onSuccess: (data) => {
setUser(data?.data.username);
},
});
return { user, isLoading }
}
const AuthContext = ({ children }: IAuthContextProps) => {
const { user, isLoading } = useAuthFetch();
return (
<>
{
isLoading ? <LoginLoading /> :
user ? children : <NonUserRouter />
}
</>
)
}
export default AuthContext;