userMe处理无token情况,节省资源
This commit is contained in:
@@ -6,7 +6,12 @@ export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) {
|
|||||||
|
|
||||||
const { data: user, isLoading, error } = useSWR(
|
const { data: user, isLoading, error } = useSWR(
|
||||||
'/api/user/me',
|
'/api/user/me',
|
||||||
async () => UserApi.me(),
|
async () => {
|
||||||
|
if (isClientSide && !localStorage.getItem('token')) {
|
||||||
|
throw Object.assign(new Error('未登录'), { statusCode: -1 });
|
||||||
|
}
|
||||||
|
return await UserApi.me();
|
||||||
|
},
|
||||||
{
|
{
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
if (error.statusCode === 401) {
|
if (error.statusCode === 401) {
|
||||||
@@ -20,7 +25,7 @@ export function useUserMe({ onError }: { onError?: (e: any) => void } = {}) {
|
|||||||
revalidateIfStale: false,
|
revalidateIfStale: false,
|
||||||
revalidateOnFocus: false,
|
revalidateOnFocus: false,
|
||||||
shouldRetryOnError: (err) => {
|
shouldRetryOnError: (err) => {
|
||||||
if (err.statusCode === 401) {
|
if ([-1, 401].includes(err.statusCode)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user