Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login Page Refactor #1054

Merged
merged 15 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/Architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
| | | | |- 📜 dndHelpers.ts
| | | | |- 📜 firebase.ts
| | | | |- 📜 dateFormat.ts
| | | | |- 📜 nameFormat.ts
| | | |- 📂 components:
| | | | |- 📜 Spacer.tsx
| | | | |- 📜 Footer.tsx
Expand Down
36 changes: 27 additions & 9 deletions frontend/src/common/redux/userLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { auth } from "@/common/utils/firebase";
import { FirebaseError } from "firebase/app";
import storage from "local-storage-fallback";
import { backendApi } from "./backendApi";
import { isNameValid } from "../utils/nameFormat";

export interface UserState {
user?: UserType | "pending";
Expand Down Expand Up @@ -208,40 +209,57 @@ export const registerViaEmailAndPassword = createAsyncThunk<
{
email: string;
password: string;
displayName: string;
passwordConfirmation: string;
firstName: string;
lastName: string;
recaptcha: string | null;
},
ThunkApiType
>(
"currentUser/registerViaEmailAndPassword",
async ({ email, password, displayName, recaptcha }, thunkAPI) => {
async ({ email, password, passwordConfirmation, firstName, lastName, recaptcha }, thunkAPI) => {
if (!recaptcha) {
return thunkAPI.rejectWithValue({
message: "Please complete the recaptcha",
message: "Please complete the recaptcha"
});
}
if (email === "") {
if (!email || email === "") {
return thunkAPI.rejectWithValue({
message: "Please enter your email",
message: "Please enter your email"
});
}
if (password === "") {
if (!password || password === "") {
return thunkAPI.rejectWithValue({
message: "Please enter your password",
message: "Please enter your password"
});
}

if (password !== passwordConfirmation) {
karkir0003 marked this conversation as resolved.
Show resolved Hide resolved
return thunkAPI.rejectWithValue({
message: "Passwords do not match"
});
}
if (displayName === "") {

if (!firstName || firstName === "" || !isNameValid(firstName)) {
return thunkAPI.rejectWithValue({
message: "Please enter your first name"
karkir0003 marked this conversation as resolved.
Show resolved Hide resolved
});
}

if (!lastName || lastName === "" || !isNameValid(lastName)) {
return thunkAPI.rejectWithValue({
message: "Please enter your display name",
message: "Please enter your last name"
});
}

try {
const userCredential = await createUserWithEmailAndPassword(
auth,
email,
password
);
const user = userCredential.user;
const displayName = firstName + ' ' + lastName;
if (displayName) {
await updateProfile(user, { displayName });
}
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/common/utils/nameFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const isNameValid = (name: string) : boolean => {
karkir0003 marked this conversation as resolved.
Show resolved Hide resolved
name = name.toLowerCase();
for (let i = 0; i < name.length; ++i) {
const c = name.charAt(i);
/* Checks if a character is a letter [a-z] or [A - Z] */
karkir0003 marked this conversation as resolved.
Show resolved Hide resolved
if (c.toLowerCase() === c.toUpperCase()) {
return false;
}
}
return true;
};
50 changes: 40 additions & 10 deletions frontend/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ const LoadingOverlay = () => {
const Login = () => {
const [isLoading, setIsLoading] = useState(false);
const [isRegistering, setIsRegistering] = useState(false);
const [fullName, setFullName] = useState<string>("");
const [firstName, setFirstName] = useState<string>("");
const [lastName, setLastName] = useState<string>("");
const [email, setEmail] = useState<string>("");
const [password, setPassword] = useState<string>("");
const [confirmPassword, setConfirmPassword] = useState<string>("");
const [recaptcha, setRecaptcha] = useState<string | null>(null);
const user = useAppSelector((state) => state.currentUser.user);
const router = useRouter();
Expand Down Expand Up @@ -148,12 +150,25 @@ const Login = () => {
<>
{isRegistering && (
<Form.Group className="mb-3" controlId="login-name">
<Form.Label>Name</Form.Label>
<Form.Control
placeholder="Enter name"
onBlur={(e) => setFullName(e.target.value)}
autoComplete="name"
/>
<div className="row">
<div className="col">
<Form.Label>First Name</Form.Label>
<Form.Control
placeholder="Enter First Name"
onBlur={(e) => setFirstName(e.target.value)}
autoComplete="name"
/>
</div>

<div className="col">
<Form.Label>Last Name</Form.Label>
<Form.Control
placeholder="Enter Last Name"
onBlur={(e) => setLastName(e.target.value)}
autoComplete="name"
/>
</div>
</div>
</Form.Group>
)}

Expand All @@ -167,7 +182,7 @@ const Login = () => {
/>
</Form.Group>

<Form.Group className="mb-5" controlId="login-password">
<Form.Group className="mb-3" controlId="login-password">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
Expand All @@ -181,6 +196,19 @@ const Login = () => {
</div>
)}
</Form.Group>

{isRegistering && (
<Form.Group className="mb-5" controlId="confirm-password">
<Form.Label>Confirm Password</Form.Label>
<Form.Control
type="password"
placeholder="Confirm Password"
onBlur={e => setConfirmPassword(e.target.value)}
autoComplete="current-password"
/>
</Form.Group>
)}

karkir0003 marked this conversation as resolved.
Show resolved Hide resolved
{isRegistering && process.env.REACT_APP_CAPTCHA_SITE_KEY && (
<div className="reCaptcha">
<ReCAPTCHA
Expand All @@ -202,11 +230,13 @@ const Login = () => {
registerViaEmailAndPassword({
email: email,
password: password,
displayName: fullName,
passwordConfirmation: confirmPassword,
firstName: firstName,
karkir0003 marked this conversation as resolved.
Show resolved Hide resolved
lastName: lastName,
recaptcha: recaptcha,
})
).unwrap();
toast.success(`Welcome ${fullName}`, {
toast.success(`Welcome ${firstName + " " + lastName}`, {
position: toast.POSITION.TOP_CENTER,
});
} catch (e) {
Expand Down
Loading