From 05eb3bfca31897747e66de4806a85635e81c281a Mon Sep 17 00:00:00 2001 From: ccbikai Date: Tue, 20 Aug 2024 21:24:32 +0800 Subject: [PATCH] feat: enhance error handling for unauthorized API calls Implements automatic token removal and redirection to login upon 401 error status, improving user experience and security. --- utils/api.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utils/api.ts b/utils/api.ts index 272d83c..fe56530 100644 --- a/utils/api.ts +++ b/utils/api.ts @@ -7,6 +7,10 @@ export function useAPI(api: string, options?: object): Promise { Authorization: `Bearer ${localStorage.getItem('SinkSiteToken') || ''}`, }, })).catch((error) => { + if (error?.status === 401) { + localStorage.removeItem('SinkSiteToken') + navigateTo('/dashboard/login') + } if (error?.data?.statusMessage) { toast(error?.data?.statusMessage) }