Преглед изворни кода

fix: 登录后 auth/current 失败时不再误报成功

sign-in 成功后对 auth/current 的调用原先没有 catch,失败时仍弹 success
且留下未处理的 rejection。改为 await + try/catch,成功才提示并跳转。
visuddhinanda пре 3 недеља
родитељ
комит
f7109e7a41
1 измењених фајлова са 26 додато и 22 уклоњено
  1. 26 22
      dashboard-v6/src/components/users/SignIn.tsx

+ 26 - 22
dashboard-v6/src/components/users/SignIn.tsx

@@ -49,32 +49,36 @@ const Widget = () => {
             "/api/v2/sign-in",
             user
           );
+          if (!res.ok) {
+            setError("用户名或密码错误");
+            return;
+          }
           //TODO sign-in 同时返回用户信息,之后直接dispatch 无需二次查询
-          if (res.ok) {
-            set(res.data);
-            get<IUserResponse>("/api/v2/auth/current").then((json) => {
-              if (json.ok) {
-                console.debug("获取用户信息成功", json.data);
-                dispatch(signIn([json.data, res.data]));
-                let url: string | null = null;
-                searchParams.forEach((value: string, key: string) => {
-                  if (key === "url") {
-                    url = value;
-                  }
-                });
-                if (url) {
-                  window.location.href = atob(url);
-                } else {
-                  navigate(TO_WORKSPACE);
-                }
-              } else {
-                setError("用户名或密码错误");
-                console.error(json.message);
+          set(res.data);
+          try {
+            const json = await get<IUserResponse>("/api/v2/auth/current");
+            if (!json.ok) {
+              setError("用户名或密码错误");
+              console.error(json.message);
+              return;
+            }
+            console.debug("获取用户信息成功", json.data);
+            dispatch(signIn([json.data, res.data]));
+            let url: string | null = null;
+            searchParams.forEach((value: string, key: string) => {
+              if (key === "url") {
+                url = value;
               }
             });
+            if (url) {
+              window.location.href = atob(url);
+            } else {
+              navigate(TO_WORKSPACE);
+            }
             message.success(intl.formatMessage({ id: "flashes.success" }));
-          } else {
-            setError("用户名或密码错误");
+          } catch (e) {
+            console.error(e);
+            setError("登录失败,请重试");
           }
         }}
       >