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

Solved issue 182 #284

Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 13 additions & 45 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,57 +33,27 @@ function Login() {
: `${process.env.REACT_APP_BASE_URL}/canteenLogin`;


// const apiUrl = `http://localhost:4000/api/v1/studentLogin`;
const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentLogin`;
// const apiUrl = `http://localhost:8000/api/v1/studentLogin`;
// // const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentLogin`;


// Assuming the response contains a token
const token = response.data.token;
try {
const response = await axios.post(apiUrl, formData);
const { token, cantId } = response.data;


localStorage.setItem("token", token);
localStorage.setItem("canteenId", cantId);

if (formData.accountType === "User") {
toast.success("User logged in successfully!");
navigate("/home");
} else {
toast.success("User Logged in ");
toast.success("User Logged in");
navigate(`/section/${cantId}`);
}
}

else{
const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenLogin`;
// const apiUrl = `http://localhost:4000/api/v1/canteenLogin`;
setLoading(true);

axios
.post(apiUrl, formData)
.then((response) => {
setLoading(false);
localStorage.setItem("canteenId", response.data.cantId);
localStorage.setItem("token", response.data.token);
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");
});

} catch (error) {
toast.error("Failed To Login. Please try again.");
console.error(error);
const errorMessage = error.response?.data?.message || "Failed to login. Please try again.";
toast.error(errorMessage);
} finally {
setLoading(false);

}
}

Expand Down Expand Up @@ -148,7 +118,7 @@ function Login() {

<div className="relative mb-4">
<input
required
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove required ?? it is important to have password

required
className="w-full py-2 px-3 border border-gray-300 rounded-2xl"
type={showPassword ? "text" : "password"}
placeholder="Password"
Expand All @@ -167,13 +137,11 @@ function Login() {
)}
</span>
</div>
<div className="mb-4 flex justify-end text-red-400">
<Link to="/forgotPassword">
<h1 classname="font-medium">
Forogt Password ?
</h1>
</Link>
</div>
<div className="mb-4 flex justify-end text-red-400">
<Link to="/forgotPassword">
<h1 className="font-medium">Forgot Password ?</h1>
</Link>
</div>

<button
type="submit"
Expand All @@ -184,7 +152,7 @@ function Login() {
</button>

<Link to="/signup">
<span className="text-sm hover:text-blue-500 cursor-pointer">
<span className="text-sm hover:text-blue-500 cursor-pointer">
Don't have an account? Sign Up
</span>
</Link>
Expand All @@ -196,4 +164,4 @@ function Login() {
);
}

export default Login;
export default Login;
6 changes: 4 additions & 2 deletions src/pages/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function Signup() {

// const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentSignup`;
const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentSignup`;
// const apiUrl = `http://localhost:8000/api/v1/studentSignUp`;
try {

setLoading(true);
Expand All @@ -97,8 +98,9 @@ function Signup() {
toast.success("Account Created Successfully!");
navigate("/home");
} catch (error) {
toast.error("Failed To Create Account. Please try again.");
console.error(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);
}
Expand Down
Loading