From 4ca8f34474483ef958ab1677447c6a72a4b3545f Mon Sep 17 00:00:00 2001 From: Kitty-001 Date: Sun, 7 Jul 2024 17:29:53 +0800 Subject: [PATCH 1/3] email and password in login and register --- frontend/src/contexts/AuthContext.js | 21 +++++++++++++++++++++ frontend/src/pages/Login.js | 11 ++++++++++- frontend/src/pages/Register.js | 12 +++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/frontend/src/contexts/AuthContext.js b/frontend/src/contexts/AuthContext.js index f290284..9890b18 100644 --- a/frontend/src/contexts/AuthContext.js +++ b/frontend/src/contexts/AuthContext.js @@ -49,6 +49,26 @@ const AuthContextProvider = ({ children }) => { } }, [authTokens]); + + /** + * validate email format + * @param {*} email + * @returns + */ + const validateEmail = (email) => { + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return emailRegex.test(email); + }; + + /** + * validate password length + * @param {*} password + * @returns + */ + const validatePassword = (password) => { + return password.length >= 4; // Ensure password is at least 4 characters long + }; + /** * Handle Login */ @@ -165,6 +185,7 @@ const AuthContextProvider = ({ children }) => { isLoggedIn, authTokens, user, setIsLoggedIn, setAuthTokens, setUser, Logout, Register, Login, refresh, + validateEmail, validatePassword, }}> {children} diff --git a/frontend/src/pages/Login.js b/frontend/src/pages/Login.js index 619710f..aa629f8 100644 --- a/frontend/src/pages/Login.js +++ b/frontend/src/pages/Login.js @@ -8,7 +8,7 @@ import { toast } from "react-toastify"; const Login = () => { let navigate = useNavigate(); - const { Login, isLoggedIn } = useAuth(); + const { Login, isLoggedIn, validateEmail, validatePassword } = useAuth(); const TokenObtainURL = process.env.REACT_APP_BACKEND_URL + "/api/token/"; useEffect(() => { @@ -22,6 +22,15 @@ const Login = () => { event.preventDefault(); const email = event.target.email.value; const password = event.target.password.value; + if (!validateEmail(email)) { + toast.error("Wrong email format"); + return; + } + if (!validatePassword(password)) { + toast.error("Password should be at least 4 characters long"); + return; + } + try { const response = await axios.post(TokenObtainURL, { email, password }); console.log("Login response get!"); diff --git a/frontend/src/pages/Register.js b/frontend/src/pages/Register.js index 5135d60..7820210 100644 --- a/frontend/src/pages/Register.js +++ b/frontend/src/pages/Register.js @@ -6,7 +6,7 @@ import axios from "axios"; const Register = () => { - const { Register, isLoggedIn } = useAuth(); + const { Register, isLoggedIn, validateEmail, validatePassword } = useAuth(); let navigate = useNavigate(); @@ -19,6 +19,16 @@ const Register = () => { const handleRegister = async (ev) => { + ev.preventDefault(); + if (!validateEmail(ev.target.email.value)) { + toast.error("Wrong email format"); + return; + } + if (!validatePassword(ev.target.password.value)) { + toast.error("Password should be at least 4 characters long"); + return; + } + Register(ev); }; From 7c660c1836384b5ad28f6a5838a930a8bc98c950 Mon Sep 17 00:00:00 2001 From: Kitty-001 Date: Sun, 7 Jul 2024 23:29:02 +0800 Subject: [PATCH 2/3] bug fic to avoid accidental duplicates --- .../dashboard/AddTransactionForm.js | 26 ++++++++++++++++++- frontend/src/contexts/DashBoardContext.js | 1 - 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/dashboard/AddTransactionForm.js b/frontend/src/components/dashboard/AddTransactionForm.js index 3d67759..a7cbc32 100644 --- a/frontend/src/components/dashboard/AddTransactionForm.js +++ b/frontend/src/components/dashboard/AddTransactionForm.js @@ -1,15 +1,39 @@ import React from "react"; import { useDash } from "../../contexts/DashBoardContext"; +import { useState } from "react"; +import { useRef } from "react"; const AddTransactionForm = () => { const { setTransactionType, handleDateChange, handleTransaction } = useDash(); const { expenseCategories, incomeCategories, transactionType, dateValue } = useDash(); + // const [formState, setFormState] = useState({ + // title: '', + // amount: '', + // type: '', + // label: '', + // // date: dateValue, + // }); + + const formRef = useRef(null); + + const cleanAfterSubmit = async (ev) => { + try { + await handleTransaction(ev); + formRef.current.reset(); + formRef.current.querySelector('select[name="type"]').value = ''; + formRef.current.querySelector('select[name="label"]').value = ''; + } catch (err) { + console.error(err); + } + + }; + return (

Add Transaction

-
+
diff --git a/frontend/src/contexts/DashBoardContext.js b/frontend/src/contexts/DashBoardContext.js index 9977cc9..bf874ff 100644 --- a/frontend/src/contexts/DashBoardContext.js +++ b/frontend/src/contexts/DashBoardContext.js @@ -17,7 +17,6 @@ const DashboardContextProvider = ({ children }) => { // Initialize with today's date return new Date().toLocaleString('en-CA', { timeZone: 'Asia/Singapore' }).split(',')[0]; }); - console.log(dateValue) const handleDateChange = (event) => { setDateValue(event.target.value); }; From 050a0f32fefdb9cc8996757bae1787548026ddc6 Mon Sep 17 00:00:00 2001 From: Kitty-001 Date: Mon, 8 Jul 2024 19:13:31 +0800 Subject: [PATCH 3/3] mergefix --- frontend/src/pages/Register.js | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/src/pages/Register.js b/frontend/src/pages/Register.js index 41ad163..4897a4d 100644 --- a/frontend/src/pages/Register.js +++ b/frontend/src/pages/Register.js @@ -4,6 +4,7 @@ import { useAuth } from "../contexts/AuthContext"; import React, { useState } from 'react'; import Popup from 'reactjs-popup'; import 'reactjs-popup/dist/index.css'; +import { toast } from "react-toastify"; const Register = () => { const { Register, isLoggedIn, validateEmail, validatePassword } = useAuth();