提升userMe到hook中
This commit is contained in:
30
tone-page-web/hooks/user/use-user-me.ts
Normal file
30
tone-page-web/hooks/user/use-user-me.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { UserApi } from "@/lib/api";
|
||||
import useSWR from "swr";
|
||||
|
||||
export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) {
|
||||
const isClientSide = typeof window !== 'undefined';
|
||||
|
||||
const { data: user, isLoading, error } = useSWR(
|
||||
'/api/user/me',
|
||||
async () => UserApi.me(),
|
||||
{
|
||||
onError: (error) => {
|
||||
if (error.statusCode === 401) {
|
||||
if (isClientSide) {
|
||||
localStorage.removeItem('token');
|
||||
}
|
||||
}
|
||||
|
||||
onError?.(error);
|
||||
},
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
user,
|
||||
isLoading,
|
||||
error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user