diff --git a/src/components/login/LoginForm.vue b/src/components/login/LoginForm.vue index 6c4e9fb0..544674e5 100644 --- a/src/components/login/LoginForm.vue +++ b/src/components/login/LoginForm.vue @@ -3,6 +3,7 @@ import { setFocus } from "@/formkit/utils/focus"; import { useUserStore } from "@/stores/user"; import { randomUUID } from "@/utils/id"; import axios from "axios"; +import { AxiosError } from "axios"; import { Toast, VButton } from "@halo-dev/components"; import { onMounted, ref } from "vue"; import qs from "qs"; @@ -54,9 +55,26 @@ const handleLogin = async () => { localStorage.setItem("logged_in", "true"); emit("succeed"); - } catch (e) { + } catch (e: unknown) { console.error("Failed to login", e); - Toast.error("登录失败,用户名或密码错误"); + + if (e instanceof AxiosError) { + if (/Network Error/.test(e.message)) { + Toast.error("网络错误,请检查网络连接"); + return; + } + + if (e.response?.status === 403) { + Toast.warning("CSRF Token 失效,请重新尝试", { duration: 5000 }); + handleGenerateToken(); + return; + } + + Toast.error("登录失败,用户名或密码错误"); + } else { + Toast.error("未知异常"); + } + loginForm.value.password = ""; setFocus("passwordInput"); } finally {