Skip to content

Commit e409841

Browse files
refactor(react-user): refactor user login and signup page (#1528)
* refactor: replace toast message with Message component for login error * refactor: update signup error * refactor: dynamic translation logic * refactor: login and signup error message * refactor: update the width of first user signup page * refactor: update signup first user page * fix: fix issue to login app user using first signup form in admin app * style: fix coding standard * style: fix coding standard
1 parent 6a1233c commit e409841

6 files changed

Lines changed: 77 additions & 32 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"errors": {
3-
"401": "The credentials you input are invalid.",
3+
"401": "Invalid credentials. Please check your email or password and try again.",
4+
"emailAlreadyExists": "This email already exists. Please sign in instead.",
45
"otherErrors": "Oops! Something went wrong."
56
}
67
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"errors": {
3-
"401": "The credentials you input are invalid (fr).",
3+
"401": "Invalid credentials. Please check your email or password and try again. (fr)",
4+
"emailAlreadyExists": "This email already exists. Please sign in instead. (fr)",
45
"otherErrors": "Oops! Something went wrong (fr)."
56
}
67
}

packages/user/src/api/user/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const signUpFirstUser = async (
2828
withCredentials: true,
2929
});
3030

31-
if (response.data.status === "ERROR") {
31+
if (response.data.status === "EMAIL_ALREADY_EXISTS_ERROR") {
3232
throw new Error(response.data.message);
3333
} else {
3434
return response.data;

packages/user/src/components/Login/LoginWrapper.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useTranslation } from "@prefabs.tech/react-i18n";
2+
import { Message } from "@prefabs.tech/react-ui";
23
import { FC, useState } from "react";
34
import { toast } from "react-toastify";
45

@@ -34,6 +35,9 @@ export const LoginWrapper: FC<IProperties> = ({
3435
const { setUser } = useUser();
3536
const config = useConfig();
3637
const [loginLoading, setLoginLoading] = useState<boolean>(false);
38+
const [loginError, setLoginError] = useState<
39+
null | "invalidCredentials" | "other"
40+
>(null);
3741

3842
const links: Array<LinkType> = [
3943
{
@@ -70,19 +74,36 @@ export const LoginWrapper: FC<IProperties> = ({
7074
}
7175
})
7276
.catch(async (error) => {
73-
const errorMessage = `errors.${error.message}`;
74-
7577
onLoginFailed && (await onLoginFailed(error));
7678

77-
toast.error(t(errorMessage, { ns: "errors" }));
79+
if (error.message === "401") {
80+
setLoginError("invalidCredentials");
81+
} else {
82+
setLoginError("other");
83+
}
7884
});
7985

8086
setLoginLoading(false);
8187
}
8288
};
8389

90+
const message =
91+
loginError === "invalidCredentials"
92+
? t("errors.401", { ns: "errors" })
93+
: t("errors.otherErrors", { ns: "errors" });
94+
8495
return (
8596
<>
97+
{loginError && (
98+
<Message
99+
enableClose={true}
100+
message={message}
101+
onClose={() => {
102+
setLoginError(null);
103+
}}
104+
severity="danger"
105+
/>
106+
)}
86107
<LoginForm
87108
handleSubmit={handleLoginSubmit}
88109
loading={handleSubmit ? loading : loginLoading}

packages/user/src/components/Signup/SignupWrapper.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useTranslation } from "@prefabs.tech/react-i18n";
2+
import { Message } from "@prefabs.tech/react-ui";
23
import React, { useState } from "react";
34
import { toast } from "react-toastify";
45

@@ -33,6 +34,10 @@ export const SignupWrapper: React.FC<IProperties> = ({
3334
}) => {
3435
const { t } = useTranslation("user");
3536
const [signupLoading, setSignupLoading] = useState<boolean>(false);
37+
const [signupError, setSignupError] = useState<
38+
null | "emailAlreadyExists" | "other"
39+
>(null);
40+
3641
const { setUser } = useUser();
3742
const config = useConfig();
3843

@@ -84,24 +89,39 @@ export const SignupWrapper: React.FC<IProperties> = ({
8489
}
8590
})
8691
.catch(async (error) => {
87-
const errorMessage = t("errors.otherErrors", { ns: "errors" });
88-
8992
onSignupFailed && (await onSignupFailed(error));
9093

91-
if (error.status === "FIELD_ERROR") {
92-
throw error as Error;
94+
if (error.message.includes("email already exists")) {
95+
setSignupError("emailAlreadyExists");
96+
97+
return;
9398
}
9499

95-
toast.error(error.message || errorMessage);
100+
setSignupError("other");
96101
})
97102
.finally(() => {
98103
setSignupLoading(false);
99104
});
100105
}
101106
};
102107

108+
const message =
109+
signupError === "emailAlreadyExists"
110+
? t("errors.emailAlreadyExists", { ns: "errors" })
111+
: t("errors.otherErrors", { ns: "errors" });
112+
103113
return (
104114
<>
115+
{signupError && (
116+
<Message
117+
enableClose={true}
118+
message={message}
119+
onClose={() => {
120+
setSignupError(null);
121+
}}
122+
severity="danger"
123+
/>
124+
)}
105125
<SignupForm
106126
handleSubmit={handleSignupSubmit}
107127
loading={handleSubmit ? loading : signupLoading}

packages/user/src/views/SignupFirstUser.tsx

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useTranslation } from "@prefabs.tech/react-i18n";
2-
import { Card, CardBody, Page } from "@prefabs.tech/react-ui";
2+
import { AuthPage, Message } from "@prefabs.tech/react-ui";
33
import { useEffect, useState } from "react";
44
import { useNavigate } from "react-router-dom";
55
import { toast } from "react-toastify";
@@ -74,38 +74,40 @@ export const SignUpFirstUser = ({
7474
})
7575
.catch(() => {
7676
setSignUpFirstUserLoading(false);
77-
toast.error(`${t("firstUser.signup.messages.error")}`);
77+
setIsError(true);
7878
});
7979
};
8080

8181
const renderPageContent = () => {
82-
if (isError) {
83-
return (
84-
<Card>
85-
<CardBody>
86-
<p>{t(`errors:errors.otherErrors`)}</p>
87-
</CardBody>
88-
</Card>
89-
);
90-
}
91-
9282
return (
93-
<SignupForm
94-
email={""}
95-
handleSubmit={handleSubmit}
96-
loading={signUpFirstUserLoading}
97-
/>
83+
<>
84+
{isError && (
85+
<Message
86+
enableClose={true}
87+
message={t("firstUser.signup.messages.error")}
88+
onClose={() => {
89+
setIsError(false);
90+
}}
91+
severity="danger"
92+
/>
93+
)}
94+
<SignupForm
95+
email={""}
96+
handleSubmit={handleSubmit}
97+
loading={signUpFirstUserLoading}
98+
/>
99+
</>
98100
);
99101
};
100102

101103
return (
102-
<Page
104+
<AuthPage
105+
centered={centered}
103106
className="signup"
104-
title={t("firstUser.title")}
105107
loading={loading || loginLoading}
106-
centered={centered}
108+
title={t("firstUser.title")}
107109
>
108110
{renderPageContent()}
109-
</Page>
111+
</AuthPage>
110112
);
111113
};

0 commit comments

Comments
 (0)