diff --git a/server/package.json b/server/package.json index 9875b1c..371e12c 100644 --- a/server/package.json +++ b/server/package.json @@ -21,6 +21,7 @@ "express-async-handler": "^1.2.0", "faker": "^5.5.3", "jsonwebtoken": "^9.0.2", + "mongodb": "^6.7.0", "mongoose": "^7.6.13", "multer": "^1.4.5-lts.1", "nodemailer": "^6.9.13", diff --git a/src/components/Navbar.jsx b/src/components/Navbar.jsx index 437efda..a6a34ba 100644 --- a/src/components/Navbar.jsx +++ b/src/components/Navbar.jsx @@ -86,6 +86,7 @@ const Navbar = () => { + {isOpen && ( { - toast.success("User Logged in "); + toast.success("User Logged in"); + if (formData.accountType === "User") { navigate("/home"); - }) - .catch((error)=>{ - toast.error("Failed to login") - }); - } - - else{ - - const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenLogin`; - - axios.post(apiUrl , formData) - .then((response)=>{ - toast.success("User Logged in "); + } else { navigate(`/section/${response.data.cantId}`); - }) - .catch((error)=>{ - toast.error("Failed to login") - }); - + } + } catch (error) { + toast.error("Failed to login"); + } finally { + setLoading(false); } } @@ -89,10 +76,8 @@ function Login() {
-
@@ -108,43 +93,6 @@ function Login() {
- -
- -
- -
- logo -

Connecting You to Your College Canteens

-
- -
-
-
-
- -
- -
- -
- -

Hello Again!

-

Welcome Back

- -
- -
- -
Welcome Back

-
-
-
-
-
-

Forgot Password ?

+

Forgot Password?

-
- -
- - setShowPassword((prev) => !prev)} - > - {showPassword ? : } - + Login + + + + Don't have an account? Sign Up + + +
- - - - - Don't have an account? Sign Up - - - - -
- -
+ + )} + ); } export default Login; - diff --git a/src/pages/Signup.jsx b/src/pages/Signup.jsx index afb10d0..582057a 100644 --- a/src/pages/Signup.jsx +++ b/src/pages/Signup.jsx @@ -1,14 +1,17 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { Link, useNavigate } from "react-router-dom"; import { AiOutlineEye, AiOutlineEyeInvisible } from "react-icons/ai"; import { toast } from "react-hot-toast"; import axios from "axios"; -import logo from "../assets/logo2.png" +import logo from "../assets/logo2.png"; import Icon from 'react-icons-kit'; -import { arrows_circle_check } from 'react-icons-kit/linea/arrows_circle_check' -import { arrows_exclamation } from 'react-icons-kit/linea/arrows_exclamation' +import { arrows_circle_check } from 'react-icons-kit/linea/arrows_circle_check'; +import { arrows_exclamation } from 'react-icons-kit/linea/arrows_exclamation'; +import { useAuth } from "../authContext.js"; function Signup() { + const { isAuthenticated, signUp } = useAuth(); + const [formData, setFormData] = useState({ name: "", email: "", @@ -18,60 +21,40 @@ function Signup() { confirmPassword: "", }); - const [showconfirmPassword, setshowconfirmPassword] = useState(false); - + const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [showPassword, setShowPassword] = useState(false); const [lowerValidated, setLowerValidated] = useState(false); const [upperValidated, setUpperValidated] = useState(false); const [numberValidated, setNumberValidated] = useState(false); const [specialValidated, setSpecialValidated] = useState(false); const [lengthValidated, setLengthValidated] = useState(false); - + const [loading, setLoading] = useState(false); const navigate = useNavigate(); + useEffect(() => { + if (isAuthenticated) { + navigate('/home'); + } + }, [isAuthenticated, navigate]); + function PasswordChecker(event) { const lower = new RegExp('(?=.*[a-z])'); const upper = new RegExp('(?=.*[A-Z])'); const number = new RegExp('(?=.*[0-9])'); const special = new RegExp('(?=.*[!@#\$%\^&\*])'); - const length = new RegExp('(?=.{8,})') + const length = new RegExp('(?=.{8,})'); const value = event.target.value; - if (lower.test(value)) { - setLowerValidated(true); - } - else { - setLowerValidated(false); - } - if (upper.test(value)) { - setUpperValidated(true); - } - else { - setUpperValidated(false); - } - if (number.test(value)) { - setNumberValidated(true); - } - else { - setNumberValidated(false); - } - if (special.test(value)) { - setSpecialValidated(true); - } - else { - setSpecialValidated(false); - } - if (length.test(value)) { - setLengthValidated(true); - } - else { - setLengthValidated(false); - } + setLowerValidated(lower.test(value)); + setUpperValidated(upper.test(value)); + setNumberValidated(number.test(value)); + setSpecialValidated(special.test(value)); + setLengthValidated(length.test(value)); setFormData((prevData) => ({ ...prevData, - [event.target.name]: event.target.value, + [event.target.name]: value, })); } @@ -82,52 +65,35 @@ function Signup() { })); } - function submitHandler(event) { + async function submitHandler(event) { event.preventDefault(); - console.log("ENV FILE",process.env.REACT_APP_BASE_URL); + console.log("ENV FILE", process.env.REACT_APP_BASE_URL); if (lowerValidated && upperValidated && numberValidated && specialValidated && lengthValidated) { - if (formData.accountType === "User") { + const apiUrl = formData.accountType === "User" + ? `${process.env.REACT_APP_BASE_URL}/studentSignup` + : `${process.env.REACT_APP_BASE_URL}/canteenSignup`; - const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentSignup`; - // const apiUrl = `http://localhost:3000/api/v1/studentSignUp`; - try { - setLoading(true); + try { + setLoading(true); - const response = await axios.post(apiUrl, formData); + const response = await axios.post(apiUrl, formData); - toast.success("Account Created Successfully!"); + toast.success("Account Created Successfully!"); + if (formData.accountType === "User") { navigate("/"); - } catch (error) { - const errorMessage = - error.response?.data?.message || - "Failed to login. Please try again."; - toast.error(errorMessage); - console.log("This is our error ", error); - } finally { - setLoading(false); } - } else { - const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenSignup`; - // const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenSignup`; - - try { - setLoading(true); - - const response = await axios.post(apiUrl, formData); + if (formData.accountType === "Canteen") { const token = response.data.token; - - localStorage.setItem("token", token); - localStorage.setItem("canteenId", response.data.cantId); - toast.success("Account Created Successfully!"); - navigate(`/section/${response.data.cantId}`); - } catch (error) { - toast.error("Failed To Create Account. Please try again."); - console.error(error); - } finally { - setLoading(false); + signUp(token); + navigate("/home"); } - + } catch (error) { + const errorMessage = error.response?.data?.message || "Failed to create account. Please try again."; + toast.error(errorMessage); + console.error(error); + } finally { + setLoading(false); } } else { toast.error("Password must pass all the criteria"); @@ -135,30 +101,27 @@ function Signup() { } return ( -
-
+
+
logo -

- Connecting You to Your College Canteens -

+

Connecting You to Your College Canteens

-
-
+
-

+

Hello There!

-

+

Create an Account

@@ -206,7 +169,7 @@ function Signup() { value={formData.accountType} className="mt-1 p-2 w-full border rounded-2xl" > - @@ -228,143 +191,66 @@ function Signup() { className="absolute right-3 top-3 cursor-pointer" onClick={() => setShowPassword((prev) => !prev)} > - {showPassword ? ( - - ) : ( - - )} + {showPassword ? : }
+
+ + setShowConfirmPassword((prev) => !prev)} + > + {showConfirmPassword ? : } + +
-
- - setshowconfirmPassword((prev) => !prev)} - > - {showconfirmPassword ? ( - - ) : ( - - )} - -
- - - - - Already have an account? Login - - - {/* - - Validation Checks for password - - */} - -
-
- {lowerValidated ? ( - - - - ) : ( - - - - )} - At least one lowercase letter +
+
+ {lowerValidated ? : } +

At least one lowercase letter

-
- {upperValidated ? ( - - - - ) : ( - - - - )} - At least one uppercase letter +
+ {upperValidated ? : } +

At least one uppercase letter

-
- {numberValidated ? ( - - - - ) : ( - - - - )} - At least one number +
+ {numberValidated ? : } +

At least one number

-
- {specialValidated ? ( - - - - ) : ( - - - - )} - At least one special character +
+ {specialValidated ? : } +

At least one special character

-
- {lengthValidated ? ( - - - - ) : ( - - - - )} - At least 8 characters +
+ {lengthValidated ? : } +

Minimum 8 characters

-
- +
+
+ +
+
+ + Already have an account? Log In + +
-
); }