Skip to content

Commit

Permalink
Merge pull request VanshKing30#284 from Saksham2k3s/feat/182-display-…
Browse files Browse the repository at this point in the history
…error-message-backend-toast

Solved issue 182
  • Loading branch information
hustlerZzZ authored Jun 11, 2024
2 parents 434b330 + 926e234 commit c029236
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 47 deletions.
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
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

0 comments on commit c029236

Please sign in to comment.