Skip to content

Commit

Permalink
added frontend validations
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedibra28 committed Nov 12, 2020
1 parent 5314a25 commit 51b8d2b
Show file tree
Hide file tree
Showing 11 changed files with 11,445 additions and 7,090 deletions.
18,359 changes: 11,301 additions & 7,058 deletions client/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"react-moment": "^0.9.7",
"react-redux": "^7.2.1",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"react-scripts": "^4.0.0",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0",
Expand Down
29 changes: 23 additions & 6 deletions client/src/components/auth/ChangePassword.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { connect } from "react-redux";
import { setAlert } from "../../actions/alert";
import { register, changePassword } from "../../actions/auth";
import PropTypes from "prop-types";
import ChangePasswordValidate from "../../validations/ChangePasswordValidate";

// Material UI Icons
import VpnKeyIcon from "@material-ui/icons/VpnKey";

const ChangePassword = ({ setAlert, changePassword, history }) => {
const [errors, setErrors] = useState({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [formData, setFormData] = useState({
password: "",
password2: "",
Expand All @@ -20,13 +23,21 @@ const ChangePassword = ({ setAlert, changePassword, history }) => {

const onSubmit = async (e) => {
e.preventDefault();
if (password !== password2) {
setAlert("Password confirmation does not match password", "danger", 5000);
} else {
setErrors(ChangePasswordValidate(formData));
setIsSubmitting(true);
};

useEffect(() => {
if (Object.keys(errors).length === 0 && isSubmitting) {
changePassword(formData, history);
// console.log(formData);
setFormData({
...formData,
password: "",
password2: "",
});
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [errors]);

return (
<div>
Expand All @@ -49,6 +60,9 @@ const ChangePassword = ({ setAlert, changePassword, history }) => {
placeholder='Enter password'
/>
</div>
{errors.password && (
<div className='form-text text-danger'>{errors.password}</div>
)}
</div>

<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
Expand All @@ -65,6 +79,9 @@ const ChangePassword = ({ setAlert, changePassword, history }) => {
placeholder='Confirm new password'
/>
</div>
{errors.password2 && (
<div className='form-text text-danger'>{errors.password2}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
<div className='input-group mx-auto d-block text-right'>
Expand Down
26 changes: 24 additions & 2 deletions client/src/components/auth/Login.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { Redirect } from "react-router-dom";
import { login } from "../../actions/auth";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import LoginValidate from "../../validations/LoginValidate";

// Material UI Icons
import VpnKeyIcon from "@material-ui/icons/VpnKey";
import EmailIcon from "@material-ui/icons/Email";

const Login = ({ login, isAuthenticated }) => {
const [errors, setErrors] = useState({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [formData, setFormData] = useState({
email: "",
password: "",
Expand All @@ -21,9 +24,22 @@ const Login = ({ login, isAuthenticated }) => {

const onSubmit = async (e) => {
e.preventDefault();
login(email, password);
setErrors(LoginValidate(formData));
setIsSubmitting(true);
};

useEffect(() => {
if (Object.keys(errors).length === 0 && isSubmitting) {
login(email, password);
setFormData({
...formData,
email: "",
password: "",
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [errors]);

// Redirect if logged in
if (isAuthenticated) {
return <Redirect to='/' />;
Expand Down Expand Up @@ -51,6 +67,9 @@ const Login = ({ login, isAuthenticated }) => {
aria-describedby='basic-addon1'
/>
</div>
{errors.email && (
<div className='form-text text-danger'>{errors.email}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
<div className='input-group '>
Expand All @@ -68,6 +87,9 @@ const Login = ({ login, isAuthenticated }) => {
aria-describedby='basic-addon1'
/>
</div>
{errors.password && (
<div className='form-text text-danger'>{errors.password}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
<div className='login-flex-helper'>
Expand Down
45 changes: 35 additions & 10 deletions client/src/components/auth/Register.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { connect } from "react-redux";
import { setAlert } from "../../actions/alert";
import { register } from "../../actions/auth";
import PropTypes from "prop-types";
import RegisterValidate from "../../validations/RegisterValidate";

// Material UI Icons
import VpnKeyIcon from "@material-ui/icons/VpnKey";
Expand All @@ -11,6 +12,8 @@ import FaceIcon from "@material-ui/icons/Face";
import SupervisorAccountIcon from "@material-ui/icons/SupervisorAccount";

const Register = ({ setAlert, register, isAuthenticated, history }) => {
const [errors, setErrors] = useState({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [formData, setFormData] = useState({
name: "",
email: "",
Expand All @@ -26,17 +29,24 @@ const Register = ({ setAlert, register, isAuthenticated, history }) => {

const onSubmit = async (e) => {
e.preventDefault();
if (password !== password2) {
setAlert("Passwords do not match", "danger", 5000);
} else {
register({ name, email, password, role, history });
}
setErrors(RegisterValidate(formData));
setIsSubmitting(true);
};

// Redirect if logged in
// if (isAuthenticated) {
// return <Redirect to="/dashboard" />;
// }
useEffect(() => {
if (Object.keys(errors).length === 0 && isSubmitting) {
register({ name, email, password, role, history });
setFormData({
...formData,
name: "",
email: "",
role: "",
password: "",
password2: "",
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [errors]);

return (
<div>
Expand All @@ -59,6 +69,9 @@ const Register = ({ setAlert, register, isAuthenticated, history }) => {
placeholder='Enter name'
/>
</div>
{errors.name && (
<div className='form-text text-danger'>{errors.name}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
<div className='input-group '>
Expand All @@ -74,6 +87,9 @@ const Register = ({ setAlert, register, isAuthenticated, history }) => {
placeholder='Enter email'
/>
</div>
{errors.email && (
<div className='form-text text-danger'>{errors.email}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
<div className='input-group '>
Expand All @@ -93,6 +109,9 @@ const Register = ({ setAlert, register, isAuthenticated, history }) => {
<option value='Admin'>Admin</option>
</select>
</div>
{errors.role && (
<div className='form-text text-danger'>{errors.role}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
<div className='input-group '>
Expand All @@ -108,6 +127,9 @@ const Register = ({ setAlert, register, isAuthenticated, history }) => {
placeholder='Enter password'
/>
</div>
{errors.password && (
<div className='form-text text-danger'>{errors.password}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
<div className='input-group '>
Expand All @@ -123,6 +145,9 @@ const Register = ({ setAlert, register, isAuthenticated, history }) => {
placeholder='Enter confirm password'
/>
</div>
{errors.password2 && (
<div className='form-text text-danger'>{errors.password2}</div>
)}
</div>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto mb-2'>
{/* <div className="form-group light">
Expand Down
8 changes: 2 additions & 6 deletions client/src/components/layout/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ const Alert = ({ alerts }) =>
alerts !== null &&
alerts.length > 0 &&
alerts.map((alert) => (
<div className='row text-center alert-msg'>
<div className='col-lg-5 col-md-6 col-sm-10 col-12 mx-auto'>
<span key={alert.id} className={`mt-2 alert alert-${alert.alertType}`}>
{alert.msg}
</span>
</div>
<div key={alert.id} className={`mt-2 alert alert-${alert.alertType}`}>
{alert.msg}
</div>
));

Expand Down
13 changes: 6 additions & 7 deletions client/src/components/layout/Spinner.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React, { Fragment } from "react";
import spinner from "./spinner.gif";

export default () => {
return (
<Fragment>
<img
className='pt-5'
src={spinner}
style={{ width: "200px", margin: "auto", display: "block" }}
alt='Loading...'
/>
<div className='text-center text-light'>
<span
className='spinner-border mt-5'
style={{ width: "200px", height: "200px" }}
></span>
</div>
</Fragment>
);
};
Binary file removed client/src/components/layout/spinner.gif
Binary file not shown.
16 changes: 16 additions & 0 deletions client/src/validations/ChangePasswordValidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default function ChangePasswordValidate(formData) {
let errors = {};
const { password, password2 } = formData;

if (!password) {
errors.password = "Password is required";
} else if (password.length < 6) {
errors.password =
"Minimum length of the password must be equal or greater than 6 characters";
} else if (!password2) {
errors.password2 = "Confirm Password is required";
} else if (password !== password2) {
errors.password2 = "Password and Confirm Password does not match";
}
return errors;
}
13 changes: 13 additions & 0 deletions client/src/validations/LoginValidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function LoginValidate(formData) {
let errors = {};
const { email, password } = formData;

if (!email) {
errors.email = "Email is required";
} else if (!/[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]/.test(email)) {
errors.email = "Invalid email";
} else if (!password) {
errors.password = "Password is required";
}
return errors;
}
24 changes: 24 additions & 0 deletions client/src/validations/RegisterValidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default function RegisterValidate(formData) {
let errors = {};
const { name, email, password, password2, role } = formData;

if (!name) {
errors.name = "Name is required";
} else if (!email) {
errors.email = "Email is required";
} else if (!/[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]/.test(email)) {
errors.email = "Invalid email";
} else if (!role) {
errors.role = "Role is required";
} else if (!password) {
errors.password = "Password is required";
} else if (password.length < 8) {
errors.password =
"Minimum length of the password must be equal or greater than 8 characters";
} else if (!password2) {
errors.password2 = "Confirm Password is required";
} else if (password !== password2) {
errors.password2 = "Password and Confirm Password does not match";
}
return errors;
}

0 comments on commit 51b8d2b

Please sign in to comment.