Added authentication step 1 / 3
This commit is contained in:
1 parent
ed78682a4c
commit
1134b83997
3 files changed
+138
-41
No files matched your search
@@ -94,7 +94,7 @@ const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => {
|
|||||||
id="login-email-input"
|
id="login-email-input"
|
||||||
label="Email"
|
label="Email"
|
||||||
isRequired={true}
|
isRequired={true}
|
||||||
placeholder="john.doe@domain.com"
|
placeholder="jordan.ellis@domain.com"
|
||||||
autoComplete="email"
|
autoComplete="email"
|
||||||
value={form.email}
|
value={form.email}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
|
|||||||
@@ -11,13 +11,14 @@ import { useParams } from "react-router-dom";
|
|||||||
import background from "../../../assets/Images/background_pattern_decorative.png";
|
import background from "../../../assets/Images/background_pattern_decorative.png";
|
||||||
import Logo from "../../../assets/icons/bwu-icon.svg?react";
|
import Logo from "../../../assets/icons/bwu-icon.svg?react";
|
||||||
import Mail from "../../../assets/icons/mail.svg?react";
|
import Mail from "../../../assets/icons/mail.svg?react";
|
||||||
|
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
|
||||||
import Check from "../../../Components/Check/Check";
|
import Check from "../../../Components/Check/Check";
|
||||||
import Button from "../../../Components/Button";
|
import Button from "../../../Components/Button";
|
||||||
import Field from "../../../Components/Inputs/Field";
|
import Field from "../../../Components/Inputs/Field";
|
||||||
import axiosInstance from "../../../Utils/axiosConfig";
|
import axiosInstance from "../../../Utils/axiosConfig";
|
||||||
import "../index.css";
|
import "../index.css";
|
||||||
|
|
||||||
const LandingPage = ({isAdmin}) => {
|
const LandingPage = ({ isAdmin, onSignup }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -34,6 +35,7 @@ const LandingPage = ({isAdmin}) => {
|
|||||||
level="secondary"
|
level="secondary"
|
||||||
label="Sign up with Email"
|
label="Sign up with Email"
|
||||||
img={<Mail />}
|
img={<Mail />}
|
||||||
|
onClick={onSignup}
|
||||||
sx={{
|
sx={{
|
||||||
width: "100%",
|
width: "100%",
|
||||||
"& svg": {
|
"& svg": {
|
||||||
@@ -54,6 +56,70 @@ const LandingPage = ({isAdmin}) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Stack gap={theme.gap.large} textAlign="center">
|
||||||
|
<Box>
|
||||||
|
<Typography component="h1">Sign Up</Typography>
|
||||||
|
<Typography>Enter your personal details</Typography>
|
||||||
|
</Box>
|
||||||
|
<Box textAlign="left">
|
||||||
|
<form noValidate spellCheck={false} onSubmit={onSubmit}>
|
||||||
|
<Field
|
||||||
|
id="register-firstname-input"
|
||||||
|
label="Name"
|
||||||
|
isRequired={true}
|
||||||
|
placeholder="Jordan"
|
||||||
|
autoComplete="given-name"
|
||||||
|
value={form.firstName}
|
||||||
|
onChange={onChange}
|
||||||
|
error={errors.firstName}
|
||||||
|
/>
|
||||||
|
<Box my={theme.gap.ml}>
|
||||||
|
<Field
|
||||||
|
id="register-lastname-input"
|
||||||
|
label="Surname"
|
||||||
|
isRequired={true}
|
||||||
|
placeholder="Ellis"
|
||||||
|
autoComplete="family-name"
|
||||||
|
value={form.lastName}
|
||||||
|
onChange={onChange}
|
||||||
|
error={errors.lastName}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</form>
|
||||||
|
</Box>
|
||||||
|
<Stack direction="row" justifyContent="space-between">
|
||||||
|
<Button
|
||||||
|
level="secondary"
|
||||||
|
label="Back"
|
||||||
|
animate="slideLeft"
|
||||||
|
img={<ArrowBackRoundedIcon />}
|
||||||
|
onClick={onBack}
|
||||||
|
sx={{
|
||||||
|
mb: theme.gap.medium,
|
||||||
|
px: theme.gap.ml,
|
||||||
|
"& svg.MuiSvgIcon-root": {
|
||||||
|
mr: theme.gap.xs,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
props={{ tabIndex: -1 }}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
level="primary"
|
||||||
|
label="Continue"
|
||||||
|
onClick={onSubmit}
|
||||||
|
disabled={(errors.firstName || errors.lastName) && true}
|
||||||
|
sx={{ width: "30%" }}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const Register = ({ isAdmin }) => {
|
const Register = ({ isAdmin }) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -77,6 +143,7 @@ const Register = ({ isAdmin }) => {
|
|||||||
role: [],
|
role: [],
|
||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
|
const [step, setStep] = useState(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchInvite = async () => {
|
const fetchInvite = async () => {
|
||||||
@@ -98,46 +165,64 @@ const Register = ({ isAdmin }) => {
|
|||||||
|
|
||||||
const handleSubmit = async (e) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const registerForm = { ...form, role: isAdmin ? ["admin"] : form.role };
|
|
||||||
const { error } = credentials.validate(registerForm, {
|
|
||||||
abortEarly: false,
|
|
||||||
context: { password: form.password },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (error) {
|
if (step === 1) {
|
||||||
// validation errors
|
const { error } = credentials.validate(
|
||||||
const newErrors = {};
|
{ firstName: form.firstName, lastName: form.lastName },
|
||||||
error.details.forEach((err) => {
|
{ abortEarly: false }
|
||||||
newErrors[err.path[0]] = err.message;
|
);
|
||||||
|
if (error) {
|
||||||
|
const newErrors = {};
|
||||||
|
error.details.forEach((err) => {
|
||||||
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
createToast({ body: error.details[0].message });
|
||||||
|
} else {
|
||||||
|
setStep(2);
|
||||||
|
}
|
||||||
|
} else if (step === 3) {
|
||||||
|
const registerForm = { ...form, role: isAdmin ? ["admin"] : form.role };
|
||||||
|
const { error } = credentials.validate(registerForm, {
|
||||||
|
abortEarly: false,
|
||||||
|
context: { password: form.password },
|
||||||
});
|
});
|
||||||
setErrors(newErrors);
|
|
||||||
createToast({
|
if (error) {
|
||||||
body:
|
// validation errors
|
||||||
error.details && error.details.length > 0
|
const newErrors = {};
|
||||||
? error.details[0].message
|
error.details.forEach((err) => {
|
||||||
: "Error validating data.",
|
newErrors[err.path[0]] = err.message;
|
||||||
});
|
});
|
||||||
} else {
|
setErrors(newErrors);
|
||||||
delete registerForm.confirm;
|
|
||||||
const action = await dispatch(register(registerForm));
|
|
||||||
if (action.payload.success) {
|
|
||||||
const token = action.payload.data;
|
|
||||||
localStorage.setItem("token", token);
|
|
||||||
navigate("/");
|
|
||||||
createToast({
|
createToast({
|
||||||
body: "Welcome! Your account was created successfully.",
|
body:
|
||||||
|
error.details && error.details.length > 0
|
||||||
|
? error.details[0].message
|
||||||
|
: "Error validating data.",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (action.payload) {
|
delete registerForm.confirm;
|
||||||
// dispatch errors
|
const action = await dispatch(register(registerForm));
|
||||||
|
if (action.payload.success) {
|
||||||
|
const token = action.payload.data;
|
||||||
|
localStorage.setItem("token", token);
|
||||||
|
navigate("/");
|
||||||
createToast({
|
createToast({
|
||||||
body: action.payload.msg,
|
body: "Welcome! Your account was created successfully.",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// unknown errors
|
if (action.payload) {
|
||||||
createToast({
|
// dispatch errors
|
||||||
body: "Unknown error.",
|
createToast({
|
||||||
});
|
body: action.payload.msg,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// unknown errors
|
||||||
|
createToast({
|
||||||
|
body: "Unknown error.",
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,7 +286,19 @@ const Register = ({ isAdmin }) => {
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<LandingPage isAdmin={isAdmin} />
|
{step === 0 ? (
|
||||||
|
<LandingPage isAdmin={isAdmin} onSignup={() => setStep(1)} />
|
||||||
|
) : step === 1 ? (
|
||||||
|
<StepOne
|
||||||
|
form={form}
|
||||||
|
errors={errors}
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
onChange={handleChange}
|
||||||
|
onBack={() => setStep(0)}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
<Box textAlign="center" p={theme.gap.large}>
|
<Box textAlign="center" p={theme.gap.large}>
|
||||||
<Typography display="inline-block">
|
<Typography display="inline-block">
|
||||||
|
|||||||
@@ -85,10 +85,10 @@
|
|||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: var(--env-var-color-3);
|
color: var(--env-var-color-3);
|
||||||
}
|
}
|
||||||
.login-page button:not(.MuiIconButton-root),
|
.auth button:not(.MuiIconButton-root),
|
||||||
.login-page .field h3.MuiTypography-root,
|
.auth .field h3.MuiTypography-root,
|
||||||
.login-page .field input,
|
.auth .field input,
|
||||||
.login-page .field .input-error,
|
.auth .field .input-error,
|
||||||
.auth p,
|
.auth p,
|
||||||
.auth span {
|
.auth span {
|
||||||
font-size: var(--env-var-font-size-medium-plus);
|
font-size: var(--env-var-font-size-medium-plus);
|
||||||
@@ -96,8 +96,8 @@
|
|||||||
.auth p {
|
.auth p {
|
||||||
color: var(--env-var-color-2-light);
|
color: var(--env-var-color-2-light);
|
||||||
}
|
}
|
||||||
.login-page .field h3.MuiTypography-root,
|
.auth .field h3.MuiTypography-root,
|
||||||
.login-page .field input {
|
.auth .field input {
|
||||||
color: var(--env-var-color-5);
|
color: var(--env-var-color-5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user