UNPKG

@auth0/nextjs-auth0

Version:
33 lines (32 loc) 780 B
"use client"; import useSWR from "swr"; export function useUser() { const { data, error, isLoading, mutate } = useSWR(process.env.NEXT_PUBLIC_PROFILE_ROUTE || "/auth/profile", (...args) => fetch(...args).then((res) => { if (!res.ok) { throw new Error("Unauthorized"); } return res.json(); })); if (error) { return { user: null, isLoading: false, error, invalidate: () => mutate() }; } if (data) { return { user: data, isLoading: false, error: null, invalidate: () => mutate() }; } return { user: data, isLoading, error, invalidate: () => mutate() }; }