Skip to content

Commit 764cb6b

Browse files
authored
fix(login): update login response handling (#478)
* fix(useFetch): add try/finally to resolve indefinite loading Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * fix(pages/login): replace handleRespWithoutNotify with handleRespWithoutAuthAndNotify Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> --------- Signed-off-by: MadDogOwner <xiaoran@xrgzs.top>
1 parent 5acc299 commit 764cb6b

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/hooks/useFetch.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export const useLoading = <T>(
1111
loading,
1212
async (...arg: any[]) => {
1313
setLoading(true)
14-
const data = await p(...arg)
15-
setLoading(false)
16-
return data
14+
try {
15+
return await p(...arg)
16+
} finally {
17+
setLoading(false)
18+
}
1719
},
1820
]
1921
}
@@ -35,9 +37,11 @@ const useListLoading = <T, K>(
3537
loading,
3638
async (key: K, ...arg: any[]) => {
3739
setLoading(() => key)
38-
const data = await p(key, ...arg)
39-
setLoading(undefined)
40-
return data
40+
try {
41+
return await p(key, ...arg)
42+
} finally {
43+
setLoading(undefined)
44+
}
4145
},
4246
]
4347
}

src/pages/login/index.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
changeToken,
2020
r,
2121
notify,
22-
handleRespWithoutNotify,
22+
handleRespWithoutAuthAndNotify,
2323
base_path,
2424
handleResp,
2525
hashPwd,
@@ -154,14 +154,20 @@ const Login = () => {
154154
username_login,
155155
controller.signal,
156156
)
157-
handleRespWithoutNotify(resp, (data) => {
158-
notify.success(t("login.success"))
159-
changeToken(data.token)
160-
to(
161-
decodeURIComponent(searchParams.redirect || base_path || "/"),
162-
true,
163-
)
164-
})
157+
handleRespWithoutAuthAndNotify(
158+
resp,
159+
(data) => {
160+
notify.success(t("login.success"))
161+
changeToken(data.token)
162+
to(
163+
decodeURIComponent(searchParams.redirect || base_path || "/"),
164+
true,
165+
)
166+
},
167+
(msg) => {
168+
notify.error(msg)
169+
},
170+
)
165171
} catch (error: unknown) {
166172
if (error instanceof Error && error.name != "AbortError")
167173
notify.error(error.message)
@@ -190,7 +196,7 @@ const Login = () => {
190196
localStorage.removeItem("password")
191197
}
192198
const resp = await data()
193-
handleRespWithoutNotify(
199+
handleRespWithoutAuthAndNotify(
194200
resp,
195201
(data) => {
196202
notify.success(t("login.success"))

0 commit comments

Comments
 (0)