BUG fix: requestRefresh

This commit is contained in:
KUN1007 2023-12-09 19:58:47 +08:00
parent 68b97d0162
commit a4d134bc05
3 changed files with 7 additions and 13 deletions

View file

@ -15,7 +15,11 @@ interface ErrorResponseData {
*/ */
export const onRequestError = async (response: Response) => { export const onRequestError = async (response: Response) => {
if (response.status === 401) { if (response.status === 401) {
Message('Unauthorized', '未认证', 'error') Message(
'Login expired, please log in again.',
'登陆过期,请重新登陆',
'error'
)
useKUNGalgameUserStore().removeToken() useKUNGalgameUserStore().removeToken()
router.push('/login') router.push('/login')
return return

View file

@ -29,7 +29,7 @@ const kunFetchRequest = async <T>(
const newResponseData = await requestRefresh(fullUrl, options) const newResponseData = await requestRefresh(fullUrl, options)
const data: T = await newResponseData.json() const data: T = await newResponseData.json()
return data return data
} else if (response.status === 233) { } else if (response.status === 233 || !response.ok) {
// Handle some known backend error // Handle some known backend error
await onRequestError(response) await onRequestError(response)
return {} as T return {} as T

View file

@ -10,17 +10,7 @@ export const requestRefresh = async (
) => { ) => {
const accessTokenResponse = await generateTokenByRefreshTokenApi() const accessTokenResponse = await generateTokenByRefreshTokenApi()
if (accessTokenResponse.code === 200 && accessTokenResponse.data.token) {
useKUNGalgameUserStore().setToken(accessTokenResponse.data.token) useKUNGalgameUserStore().setToken(accessTokenResponse.data.token)
} else {
Message(
'Login expired, please log in again.',
'登陆过期,请重新登陆',
'error'
)
useKUNGalgameUserStore().removeToken()
router.push('/login')
}
const headers = { const headers = {
...options.headers, ...options.headers,