Skip to content

Commit

Permalink
Merge pull request #272 from anuragnegi000/main
Browse files Browse the repository at this point in the history
Fixed the signup and login issue #255
  • Loading branch information
hustlerZzZ authored Jun 8, 2024
2 parents 3066737 + fca2f7e commit 0ec7ce8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 73 deletions.
76 changes: 49 additions & 27 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,47 @@ function Login() {

async function submitHandler(event) {
event.preventDefault();
setLoading(true);

try {
const apiUrl =
formData.accountType === "User"
? `http://localhost:8000/api/v1/studentLogin`
: `http://localhost:8000/api/v1/canteenLogin`;
if (formData.accountType === "User") {
//Loader will show till the api fetching is done as show as promise is resolved the loader will be not shown
setLoading(true);

const response = await axios.post(apiUrl, formData);
// const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentLogin`;
const apiUrl = `http://localhost:3000/api/v1/studentLogin`;

// Assuming the response contains a token
const token = response.data.token;
try {
setLoading(true);

localStorage.setItem("token", token);
localStorage.setItem('canteenId', response.data.cantId);
if (formData.accountType === "User") {
toast.success("User logged in successfully!");
const response = await axios.post(apiUrl, formData);

toast.success("Unable to login!");
navigate("/home");
} else {
toast.success("Canteen logged in successfully!");
navigate(`/section/${response.data.cantId}`);
} catch (error) {
toast.error("Failed To Login. Please try again.");
console.error(error);
} finally {
setLoading(false);
}
} catch (error) {
toast.error("Failed to login. Please try again.");
console.error(error);
} finally {
setLoading(false);
}

else{
const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenLogin`;
setLoading(true);

axios
.post(apiUrl, formData)
.then((response) => {
setLoading(false);
toast.success("User Logged in ");
navigate(
`/section/${response.data.cantId}`
);
})
.catch((error) => {
//Loader will show till the api fetching is done as show as promise is resolved the loader will be not shown
setLoading(false);
toast.error("Failed to login");
});
}
}

Expand All @@ -64,9 +78,14 @@ function Login() {
<div className="h-screen md:flex">
<div className="relative overflow-hidden md:flex w-1/2 bg-gradient-to-t from-blue-950 via-blue-950 to-gray-900 bg-no-repeat justify-around items-center hidden">
<div>
<img src={logo} alt="logo" className="w-48 h-12 mb-2" />
<img
src={logo}
alt="logo"
className="w-48 h-12 mb-2"
/>
<p className="text-white mt-1 ml-3">
Connecting You to Your College Canteens
Connecting You to Your College
Canteens
</p>
</div>
<div className="absolute -bottom-32 -left-40 w-80 h-80 border-4 rounded-full border-opacity-30 border-t-8"></div>
Expand All @@ -78,10 +97,13 @@ function Login() {
<div className="flex md:w-1/2 justify-center py-10 items-center bg-white">
<form
className="bg-white p-8 rounded shadow-lg w-80"
onSubmit={submitHandler}
>
<h1 className="text-gray-800 font-bold text-2xl mb-1">Hello Again!</h1>
<p className="text-sm font-normal text-gray-600 mb-7">Welcome Back</p>
onSubmit={submitHandler}>
<h1 className="text-gray-800 font-bold text-2xl mb-1">
Hello Again!
</h1>
<p className="text-sm font-normal text-gray-600 mb-7">
Welcome Back
</p>

<div className="mb-4">
<input
Expand Down
92 changes: 46 additions & 46 deletions src/pages/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,53 +77,53 @@ function Signup() {
async function submitHandler(event) {
event.preventDefault();

if (
lowerValidated &&
upperValidated &&
numberValidated &&
specialValidated &&
lengthValidated
) {
if (formData.accountType === "User") {
// const apiUrl = `http://localhost:8000/api/v1/studentSignup`;
const apiUrl = `http://localhost:8000/api/v1/studentSignup`;
try {
setLoading(true);
if (
lowerValidated &&
upperValidated &&
numberValidated &&
specialValidated &&
lengthValidated
) {
if (formData.accountType === "User") {
// const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentSignup`;
const apiUrl = `http://localhost:3000/api/v1/studentSignup`;
try {
setLoading(true);

const response = await axios.post(apiUrl, formData);

toast.success("Account Created Successfully!");
navigate("/home");
} catch (error) {
toast.error("Failed To Create Account. Please try again.");
console.error(error);
} finally {
setLoading(false);
}
} else {
const apiUrl = `http://localhost:3000/api/v1/canteenSignup`
// const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenSignup`;
try {
setLoading(true);

const response = await axios.post(apiUrl, formData);

toast.success("Account Created Successfully!");
navigate("/home");
} catch (error) {
toast.error("Failed To Create Account. Please try again.");
console.error(error);
} finally {
setLoading(false);
}

const response = await axios.post(apiUrl, formData);

toast.success("Account Created Successfully!");
navigate("/home");
} catch (error) {
toast.error("Failed To Create Account. Please try again.");
console.error(error);
} finally {
setLoading(false);
}
} else {
const apiUrl = `http://localhost:8000/api/v1/canteenSignup`;
// const apiUrl = `http://localhost:8000/api/v1/canteenSignup`;
try {
setLoading(true);

const response = await axios.post(apiUrl, formData);
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);
}
}
} else {
toast.error("Password must pass all the criteria");
}
}
}
} else {
toast.error(
"Password must pass all the criteria"
);
}
}

return (
<>
Expand Down

0 comments on commit 0ec7ce8

Please sign in to comment.