Spaces:
Runtime error
Runtime error
"use client"; | |
import { useCookie, useUpdateEffect } from "react-use"; | |
import { useQuery } from "@tanstack/react-query"; | |
export const useUser = () => { | |
const [value, setValue, remove] = useCookie("auth_hf_token"); | |
const { | |
data: user, | |
refetch, | |
isLoading: loading, | |
remove: clear, | |
}: any = useQuery( | |
["user.me"], | |
async () => { | |
if (!value) return null; | |
const request = await fetch("/api/me", { | |
method: "GET", | |
headers: { | |
Authorization: `Bearer ${value}`, | |
}, | |
}) | |
const res = await request.clone().json().catch(() => ({})); | |
if (res.status === 401) { | |
remove(); | |
clear(); | |
return null; | |
} | |
return res?.user; | |
}, | |
{ | |
refetchOnWindowFocus: false, | |
} | |
); | |
useUpdateEffect(() => { | |
if (value) { | |
refetch() | |
} | |
} | |
, [value]); | |
const openWindowLogin = async () => { | |
const response = await fetch(`/api/login`); | |
const { ok, redirect } = await response.json(); | |
if (ok && redirect) { | |
window.open(redirect, "_blank"); | |
} | |
}; | |
const getAuthorization = async (code: string) => { | |
const request = await fetch("/api/auth", { | |
method: "POST", | |
body: JSON.stringify({ | |
code, | |
}), | |
}); | |
const res = await request.clone().json().catch(() => ({})); | |
if (!res.ok) return | |
setValue(res.access_token, { | |
expires: res.experes_in, | |
}); | |
} | |
return { | |
openWindowLogin, | |
user, | |
refetch, | |
loading, | |
getAuthorization | |
} | |
} |