UNPKG

@mikandev/next-discord-auth

Version:
34 lines (33 loc) 1.03 kB
"use client"; import { jsx as _jsx } from "react/jsx-runtime"; import {} from "./index"; import { createContext, useContext } from "react"; import useSWR from "swr"; const UserInfoContext = createContext(undefined); const fetcher = async (url) => { const res = await fetch(url); const data = (await res.json()); if ("error" in data && data.error) { return null; } return data; }; export function UserInfoProvider({ children, path, }) { const { data, error, isLoading, mutate } = useSWR(path, fetcher, { revalidateOnFocus: false, revalidateOnReconnect: true, }); return (_jsx(UserInfoContext.Provider, { value: { session: data ?? null, isLoading, isError: !!error, mutate, }, children: children })); } export function useUserInfo() { const context = useContext(UserInfoContext); if (context === undefined) { throw new Error("useUserInfo must be used within a UserInfoProvider"); } return context; }