[d7b7f00] | 1 | import React, { useState } from 'react';
|
---|
| 2 | import loginStyles from '../../css/AuthenticationCss/login-style.module.css';
|
---|
| 3 | import registerStyles from '../../css/AuthenticationCss/register-style.module.css';
|
---|
| 4 | import logo from '../../images/logo.png';
|
---|
| 5 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
---|
| 6 | import { faEye, faEyeSlash } from '@fortawesome/free-regular-svg-icons';
|
---|
| 7 | import { Link } from 'react-router-dom';
|
---|
| 8 | import PostRegister from './PostRegister';
|
---|
| 9 |
|
---|
| 10 | const Register = () => {
|
---|
| 11 | const [firstName, setFirstName] = useState('');
|
---|
| 12 | const [lastName, setLastName] = useState('');
|
---|
| 13 | const [showPassword, setShowPassword] = useState(false);
|
---|
| 14 | const [password, setPassword] = useState('');
|
---|
| 15 | const [confirmPassword, setConfirmPassword] = useState('');
|
---|
| 16 | const [email, setEmail] = useState('');
|
---|
| 17 | const [passwordStrength, setPasswordStrength] = useState('');
|
---|
| 18 | const [passwordIndicatorColor, setPasswordIndicatorColor] = useState('');
|
---|
| 19 | const [emailIsValid, setEmailIsValid] = useState(true);
|
---|
| 20 | const [hasBeenRegistered, setHasBeenRegistered] = useState(false);
|
---|
| 21 |
|
---|
| 22 | const capitalizeFirstLetter = (string) => {
|
---|
| 23 | return string.charAt(0).toUpperCase() + string.slice(1);
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | const handleNameChange = (e) => {
|
---|
| 27 | setFirstName(capitalizeFirstLetter(e.target.value));
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | const handleSurnameChange = (e) => {
|
---|
| 31 | setLastName(capitalizeFirstLetter(e.target.value));
|
---|
| 32 | };
|
---|
| 33 |
|
---|
| 34 | const togglePasswordVisibility = () => {
|
---|
| 35 | setShowPassword(!showPassword);
|
---|
| 36 | };
|
---|
| 37 |
|
---|
| 38 | const handleEmailChange = (e) => {
|
---|
| 39 | const emailValue = e.target.value;
|
---|
| 40 | setEmail(emailValue);
|
---|
| 41 |
|
---|
| 42 | const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
---|
| 43 | setEmailIsValid(emailRegex.test(emailValue));
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | const handlePasswordChange = (e) => {
|
---|
| 47 | setPassword(e.target.value);
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | const handleConfirmPasswordChange = (e) => {
|
---|
| 51 | setConfirmPassword(e.target.value);
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | const validatePassword = () => {
|
---|
| 55 | let strengthMessage = '';
|
---|
| 56 | let strengthColor = '';
|
---|
| 57 | let isStrongPassword = false;
|
---|
| 58 |
|
---|
| 59 | if (password.length < 6) {
|
---|
| 60 | strengthMessage = 'Password must be at least 6 characters';
|
---|
| 61 | strengthColor = 'red';
|
---|
| 62 | } else {
|
---|
| 63 | const hasUpperCase = /[A-Z]/.test(password);
|
---|
| 64 | const hasLowerCase = /[a-z]/.test(password);
|
---|
| 65 | const hasNumbers = /\d/.test(password);
|
---|
| 66 | const hasSpecialChars = /[!@#$%^&*(),.?":{}|<>]/.test(password);
|
---|
| 67 |
|
---|
| 68 | if (hasUpperCase && hasLowerCase && hasNumbers && hasSpecialChars) {
|
---|
| 69 | isStrongPassword = true;
|
---|
| 70 | } else {
|
---|
| 71 | strengthMessage = 'Password should include uppercase, lowercase, numbers, and special characters';
|
---|
| 72 | strengthColor = 'orange';
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | setPasswordStrength(strengthMessage);
|
---|
| 77 | setPasswordIndicatorColor(strengthColor);
|
---|
| 78 |
|
---|
| 79 | return isStrongPassword;
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | const handleSubmit = async (e) => {
|
---|
| 83 | e.preventDefault();
|
---|
| 84 |
|
---|
| 85 | const isStrongPassword = validatePassword();
|
---|
| 86 |
|
---|
| 87 | if (password !== confirmPassword) {
|
---|
| 88 | setPasswordStrength('Passwords do not match');
|
---|
| 89 | setPasswordIndicatorColor('red');
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (!isStrongPassword) {
|
---|
| 94 | return;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if (!emailIsValid) {
|
---|
| 98 | return;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | try {
|
---|
| 102 | const response = await fetch('http://localhost:8080/api/register', {
|
---|
| 103 | method: 'POST',
|
---|
| 104 | headers: {
|
---|
| 105 | 'Content-Type': 'application/json',
|
---|
| 106 | },
|
---|
| 107 | body: JSON.stringify({
|
---|
| 108 | email,
|
---|
| 109 | password,
|
---|
| 110 | name: firstName,
|
---|
| 111 | surname: lastName,
|
---|
| 112 | }),
|
---|
| 113 | });
|
---|
| 114 |
|
---|
| 115 | if (response.ok) {
|
---|
| 116 | setHasBeenRegistered(true);
|
---|
| 117 | } else {
|
---|
| 118 | const errorText = await response.text();
|
---|
| 119 | setPasswordStrength(errorText);
|
---|
| 120 | setPasswordIndicatorColor('red');
|
---|
| 121 | }
|
---|
| 122 | } catch (error) {
|
---|
| 123 | console.error('Error during registration:', error);
|
---|
| 124 | setPasswordStrength('An error occurred during registration. Please try again.');
|
---|
| 125 | setPasswordIndicatorColor('red');
|
---|
| 126 | }
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | return (
|
---|
| 130 | <>
|
---|
| 131 | {hasBeenRegistered ? (
|
---|
| 132 | <PostRegister />
|
---|
| 133 | ) : (
|
---|
| 134 | <div className={loginStyles.loginContainer}>
|
---|
| 135 | <div className={loginStyles.backgroundContainer}></div>
|
---|
| 136 | <div className={loginStyles.overlay}>
|
---|
| 137 | <form className={loginStyles.form} id="register-form" onSubmit={handleSubmit}>
|
---|
| 138 | <img src={logo} alt="Logo" className={loginStyles.logo} />
|
---|
| 139 | <p className={loginStyles.title}>Create an Account</p>
|
---|
| 140 | <p className={registerStyles.message}>Signup now and get full access to our app.</p>
|
---|
| 141 | <div className={registerStyles.flex}>
|
---|
| 142 | <label>
|
---|
| 143 | <input
|
---|
| 144 | className={loginStyles.input}
|
---|
| 145 | type="text"
|
---|
| 146 | value={firstName}
|
---|
| 147 | onChange={handleNameChange}
|
---|
| 148 | required
|
---|
| 149 | />
|
---|
| 150 | <span>Firstname</span>
|
---|
| 151 | </label>
|
---|
| 152 | <label>
|
---|
| 153 | <input
|
---|
| 154 | className={loginStyles.input}
|
---|
| 155 | type="text"
|
---|
| 156 | value={lastName}
|
---|
| 157 | onChange={handleSurnameChange}
|
---|
| 158 | required
|
---|
| 159 | />
|
---|
| 160 | <span>Lastname</span>
|
---|
| 161 | </label>
|
---|
| 162 | </div>
|
---|
| 163 |
|
---|
| 164 | <label>
|
---|
| 165 | <input
|
---|
| 166 | className={`${loginStyles.input} ${emailIsValid ? '' : registerStyles.invalid}`}
|
---|
| 167 | type="email"
|
---|
| 168 | value={email}
|
---|
| 169 | onChange={handleEmailChange}
|
---|
| 170 | required
|
---|
| 171 | />
|
---|
| 172 | <span>Email</span>
|
---|
| 173 | </label>
|
---|
| 174 |
|
---|
| 175 | <label>
|
---|
| 176 | <input
|
---|
| 177 | className={`${loginStyles.input} ${loginStyles.passwordInput}`}
|
---|
| 178 | type={showPassword ? 'text' : 'password'}
|
---|
| 179 | id="password"
|
---|
| 180 | value={password}
|
---|
| 181 | onChange={handlePasswordChange}
|
---|
| 182 | required
|
---|
| 183 | />
|
---|
| 184 | <span>Password</span>
|
---|
| 185 | <span className={loginStyles.eye} onClick={togglePasswordVisibility}>
|
---|
| 186 | <FontAwesomeIcon icon={showPassword ? faEyeSlash : faEye} id="show-password" />
|
---|
| 187 | </span>
|
---|
| 188 | </label>
|
---|
| 189 | <label>
|
---|
| 190 | <input
|
---|
| 191 | className={`${loginStyles.input} ${loginStyles.passwordInput}`}
|
---|
| 192 | type={showPassword ? 'text' : 'password'}
|
---|
| 193 | id="confirm-password"
|
---|
| 194 | value={confirmPassword}
|
---|
| 195 | onChange={handleConfirmPasswordChange}
|
---|
| 196 | required
|
---|
| 197 | />
|
---|
| 198 | <span>Confirm password</span>
|
---|
| 199 | </label>
|
---|
| 200 | <div id="password-indicator" className={registerStyles.passwordIndicator} style={{ color: passwordIndicatorColor }}>
|
---|
| 201 | {passwordStrength}
|
---|
| 202 | </div>
|
---|
| 203 | <button className={loginStyles.submit} type="submit">
|
---|
| 204 | Create account
|
---|
| 205 | </button>
|
---|
| 206 | <p className={loginStyles.signin}>
|
---|
| 207 | Already have an account? <Link to="/Login">Log into existing account</Link>
|
---|
| 208 | </p>
|
---|
| 209 | </form>
|
---|
| 210 | </div>
|
---|
| 211 | </div>
|
---|
| 212 | )}
|
---|
| 213 | </>
|
---|
| 214 | );
|
---|
| 215 | };
|
---|
| 216 |
|
---|
| 217 | export default Register;
|
---|