Skip to content

Commit

Permalink
Modified Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aadarsh012 committed Sep 19, 2021
1 parent 5125a61 commit 60465c4
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 33 deletions.
7 changes: 7 additions & 0 deletions client/src/Components/ForgotPassword/ForgotPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const ForgotPassword = (props) => {
const [success, setSuccess] = useState(false);
const [error, setError] = useState("");

useEffect(() => {
if (localStorage.getItem("authToken")) {
props.history.push("/");
}
}, [props.history]);

const sendEmailHandler = async (event) => {
event.preventDefault();
const config = {
Expand All @@ -17,6 +23,7 @@ const ForgotPassword = (props) => {

try {
const data = await axios.post("/api/auth/forgotpassword", { email }, config);
localStorage.setItem("resetToken", data.data.resetToken);
setSuccess(data.data);
} catch (error) {
console.log(error.response.data.error);
Expand Down
6 changes: 6 additions & 0 deletions client/src/Components/Login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const Login = (props) => {
const [password, setPassword] = useState("");
const [error, setError] = useState("");

useEffect(() => {
if (localStorage.getItem("authToken")) {
props.history.push("/");
}
}, [props.history]);

const loginHandler = async (event) => {
event.preventDefault();

Expand Down
18 changes: 10 additions & 8 deletions client/src/Components/Register/Register.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { useState, useEffect } from "react";
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import classes from "./Register.module.css";

Expand All @@ -9,9 +9,11 @@ const Register = (props) => {
const [password, setPassword] = useState("");
const [error, setError] = useState("");

// useEffect(()=>{
// if(!localStorage.getItem(authToken))
// },[props.history])
useEffect(() => {
if (localStorage.getItem("authToken")) {
props.history.push("/");
}
}, [props.history]);

const registerHandler = async (event) => {
event.preventDefault();
Expand All @@ -27,9 +29,7 @@ const Register = (props) => {
setTimeout(() => {
setError("");
}, 2000);
}

if (password.length < 8) {
} else if (password.length < 8) {
setError("Weak Password");
setTimeout(() => {
setError("");
Expand All @@ -44,7 +44,9 @@ const Register = (props) => {
);
localStorage.setItem("authToken", data.token);
props.history.push("/");
} catch (error) {}
} catch (error) {
setError(error.response.data.error);
}
};

return (
Expand Down
64 changes: 42 additions & 22 deletions client/src/Components/ResetPassword/ResetPassword.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import axios from "axios";
import { useState, useEffect } from "react";
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import classes from "./ResetPassword.module.css";

const ResetPassword = (props) => {
const [password, setPassword] = useState("");
const [success, setSuccess] = useState(false);
const [error, setError] = useState("");
const [errorPage, setErrorPage] = useState(false);

useEffect(() => {
const resetToken = localStorage.getItem("resetToken");
if (resetToken !== props.match.params.resetToken) {
localStorage.removeItem("resetToken");
setErrorPage(true);
}
}, [props.match]);

const resetPasswordHandler = async (event) => {
event.preventDefault();
Expand All @@ -24,8 +33,8 @@ const ResetPassword = (props) => {
config
);
setSuccess(data.data);
localStorage.removeItem("resetToken");
} catch (error) {
console.log(error.response.data.error);
setSuccess(false);
setError("Weak Password!");
setTimeout(() => {
Expand All @@ -34,25 +43,36 @@ const ResetPassword = (props) => {
}
};

return (
<div className={classes.resetPassword}>
<span className={classes.error}>{error}</span>
<form onSubmit={resetPasswordHandler} className={classes.resetPassword__form}>
<label htmlFor="resetPassword__password">New Password</label>
<input
id="resetPassword__password"
type="text"
value={password}
onChange={(event) => setPassword(event.target.value)}
/>
{success && (
<span>
Successful! <Link to="/login">Login</Link>
</span>
)}
<button type="submit">Reset</button>
</form>
</div>
);
if (errorPage) {
return (
<div className={classes.errorPage}>
<h2>IT SEEMS LIKE THE PASSWORD HAS ALREADY BEEN RESET.</h2>
<p>
Please <Link to="/login">click here</Link> to Login.
</p>
</div>
);
} else {
return (
<div className={classes.resetPassword}>
<span className={classes.error}>{error}</span>
<form onSubmit={resetPasswordHandler} className={classes.resetPassword__form}>
<label htmlFor="resetPassword__password">New Password</label>
<input
id="resetPassword__password"
type="text"
value={password}
onChange={(event) => setPassword(event.target.value)}
/>
{success && (
<span>
Successful! <Link to="/login">Login</Link>
</span>
)}
<button type="submit">Reset</button>
</form>
</div>
);
}
};
export default ResetPassword;
20 changes: 20 additions & 0 deletions client/src/Components/ResetPassword/ResetPassword.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@
font-weight: bold;
}

.errorPage {
display: flex;
flex-direction: column;
color: #5cdb95;
background-color: #edf5e1;
box-shadow: inset 4px 4px 8px 0 rgba(255, 255, 255, 1), 2px 2px 5px 0 rgba(0, 0, 0, 0.5);
height: 10em;
padding: 30px;
border-radius: 10px;
}

.errorPage p {
color: #05305c;
}

@media (max-width: 500px) {
.resetPassword {
width: 17em;
Expand All @@ -77,4 +92,9 @@
width: 10em;
margin: 15px 0;
}

.errorPage {
width: 15em;
height: 14em;
}
}
2 changes: 1 addition & 1 deletion client/src/Components/routes/PrivateRoute.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Switch, Route, Redirect } from "react-router-dom";
import { Route, Redirect } from "react-router-dom";
import Private from "../PrivateComponent/Private";

const PrivateRoute = (props) => {
Expand Down
4 changes: 2 additions & 2 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ exports.forgotPassword = async (req, res, next) => {
subject: "Reset Password",
text: message
});
res.status(200).json({ success: true, data: "Email Sent" });
res.status(200).json({ success: true, resetToken });
} catch (error) {
user.resetPasswordToken = undefined;
user.resetPasswordDate = undefined;
Expand Down Expand Up @@ -105,7 +105,7 @@ exports.resetPassword = async (req, res, next) => {

await user.save();

res.status(200).json({ success: true, data: "Password has been reset." });
res.status(200).json({ success: true, data: "password Reset" });
} catch (error) {
next(error);
}
Expand Down

0 comments on commit 60465c4

Please sign in to comment.