From 55512a2eae27141506183b2262de942eb2b42bdd Mon Sep 17 00:00:00 2001 From: ayan4m1 Date: Thu, 15 Jun 2023 08:48:54 -0400 Subject: [PATCH 1/2] wrap login call in try/catch for error handling --- src/shared/components/home/login.tsx | 56 +++++++++++++++------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index 381c13bb..3ddb4bc1 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -160,38 +160,42 @@ export class Login extends Component { if (username_or_email && password) { i.setState({ loginRes: { state: "loading" } }); - const loginRes = await HttpService.client.login({ - username_or_email, - password, - totp_2fa_token, - }); - switch (loginRes.state) { - case "failed": { - if (loginRes.msg === "missing_totp_token") { - i.setState({ showTotp: true }); - toast(i18n.t("enter_two_factor_code"), "info"); + try { + const loginRes = await HttpService.client.login({ + username_or_email, + password, + totp_2fa_token, + }); + switch (loginRes.state) { + case "failed": { + if (loginRes.msg === "missing_totp_token") { + i.setState({ showTotp: true }); + toast(i18n.t("enter_two_factor_code"), "info"); + } + + i.setState({ loginRes: { state: "empty" } }); + break; } - i.setState({ loginRes: { state: "empty" } }); - break; - } + case "success": { + UserService.Instance.login(loginRes.data); + const site = await HttpService.client.getSite({ + auth: myAuth(), + }); - case "success": { - UserService.Instance.login(loginRes.data); - const site = await HttpService.client.getSite({ - auth: myAuth(), - }); + if (site.state === "success") { + UserService.Instance.myUserInfo = site.data.my_user; + } - if (site.state === "success") { - UserService.Instance.myUserInfo = site.data.my_user; + i.props.history.action === "PUSH" + ? i.props.history.back() + : i.props.history.replace("/"); + + break; } - - i.props.history.action === "PUSH" - ? i.props.history.back() - : i.props.history.replace("/"); - - break; } + } catch (error) { + i.setState({ loginRes: { state: "failed", msg: error.message } }); } } } From 93e359832895e9bf3662e3511fbf4b74ade57f8c Mon Sep 17 00:00:00 2001 From: ayan4m1 Date: Fri, 16 Jun 2023 10:03:31 -0400 Subject: [PATCH 2/2] handle login failures correctly --- src/shared/components/home/login.tsx | 62 +++++++++++++--------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index 3ddb4bc1..841a3d2b 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -160,42 +160,38 @@ export class Login extends Component { if (username_or_email && password) { i.setState({ loginRes: { state: "loading" } }); - try { - const loginRes = await HttpService.client.login({ - username_or_email, - password, - totp_2fa_token, - }); - switch (loginRes.state) { - case "failed": { - if (loginRes.msg === "missing_totp_token") { - i.setState({ showTotp: true }); - toast(i18n.t("enter_two_factor_code"), "info"); - } - - i.setState({ loginRes: { state: "empty" } }); - break; + const loginRes = await HttpService.client.login({ + username_or_email, + password, + totp_2fa_token, + }); + switch (loginRes.state) { + case "failed": { + if (loginRes.msg === "missing_totp_token") { + i.setState({ showTotp: true }); + toast(i18n.t("enter_two_factor_code"), "info"); } - case "success": { - UserService.Instance.login(loginRes.data); - const site = await HttpService.client.getSite({ - auth: myAuth(), - }); - - if (site.state === "success") { - UserService.Instance.myUserInfo = site.data.my_user; - } - - i.props.history.action === "PUSH" - ? i.props.history.back() - : i.props.history.replace("/"); - - break; - } + i.setState({ loginRes: { state: "failed", msg: loginRes.msg } }); + break; + } + + case "success": { + UserService.Instance.login(loginRes.data); + const site = await HttpService.client.getSite({ + auth: myAuth(), + }); + + if (site.state === "success") { + UserService.Instance.myUserInfo = site.data.my_user; + } + + i.props.history.action === "PUSH" + ? i.props.history.back() + : i.props.history.replace("/"); + + break; } - } catch (error) { - i.setState({ loginRes: { state: "failed", msg: error.message } }); } } }