[d7b7f00] | 1 | import React, { useState, useEffect } from 'react';
|
---|
| 2 | import styles from '../../css/AuthenticationCss/login-style.module.css';
|
---|
| 3 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
---|
| 4 | import { faEye, faEyeSlash } from '@fortawesome/free-regular-svg-icons';
|
---|
| 5 | import logo from '../../images/logo.png';
|
---|
| 6 | import { Link, useNavigate } from 'react-router-dom';
|
---|
| 7 |
|
---|
| 8 | const Login = () => {
|
---|
| 9 | const [email, setEmail] = useState('');
|
---|
| 10 | const [password, setPassword] = useState('');
|
---|
| 11 | const [showPassword, setShowPassword] = useState(false);
|
---|
| 12 | const [isLoading, setIsLoading] = useState(false);
|
---|
| 13 | const [errorMessage, setErrorMessage] = useState('');
|
---|
| 14 |
|
---|
| 15 | const navigate = useNavigate();
|
---|
| 16 | useEffect(() => {
|
---|
| 17 | const params = new URLSearchParams(window.location.search);
|
---|
| 18 | const token = params.get('token');
|
---|
| 19 | const firstName = params.get('firstName');
|
---|
| 20 | const lastName = params.get('lastName');
|
---|
| 21 | const email = params.get('email');
|
---|
| 22 | const phoneNumber = params.get('phoneNumber');
|
---|
| 23 |
|
---|
| 24 | if (token) {
|
---|
| 25 | localStorage.setItem('token', token);
|
---|
| 26 | localStorage.setItem('userName', firstName || '');
|
---|
| 27 | localStorage.setItem('userSurname', lastName || '');
|
---|
| 28 | localStorage.setItem('email', email || '');
|
---|
| 29 | localStorage.setItem('phoneNumber', phoneNumber || '');
|
---|
| 30 | localStorage.setItem('address', '');
|
---|
| 31 |
|
---|
| 32 | navigate('/');
|
---|
| 33 | }
|
---|
| 34 | }, [navigate]);
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | useEffect(() => {
|
---|
| 38 | const emailField = document.querySelector(`.${styles.emailInput}`);
|
---|
| 39 | const emailLabel = emailField.nextElementSibling;
|
---|
| 40 | const passwordFields = document.querySelectorAll(`.${styles.passwordInput}`);
|
---|
| 41 | const passwordLabels = document.querySelectorAll(`.${styles.passwordInput} + span`);
|
---|
| 42 |
|
---|
| 43 | const handleEmailValidation = () => {
|
---|
| 44 | const emailValue = emailField.value;
|
---|
| 45 | const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
---|
| 46 |
|
---|
| 47 | if (!emailRegex.test(emailValue)) {
|
---|
| 48 | emailLabel.style.color = 'red';
|
---|
| 49 | } else {
|
---|
| 50 | emailField.style.borderColor = '#ccc';
|
---|
| 51 | emailLabel.style.color = '#FFA500';
|
---|
| 52 | }
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | const handleFocus = (label) => {
|
---|
| 56 | label.style.top = '3px';
|
---|
| 57 | label.style.fontSize = '0.85em';
|
---|
| 58 | label.style.fontWeight = '600';
|
---|
| 59 | label.style.transform = 'translateY(0)';
|
---|
| 60 | label.style.color = '#FFA500';
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | const handleBlur = (field, label) => {
|
---|
| 64 | if (!field.value) {
|
---|
| 65 | label.style.top = '50%';
|
---|
| 66 | label.style.fontSize = '1.1em';
|
---|
| 67 | label.style.fontWeight = 'normal';
|
---|
| 68 | label.style.transform = 'translateY(-50%)';
|
---|
| 69 | label.style.color = 'rgba(51, 51, 51, 0.5)';
|
---|
| 70 | }
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | emailField.addEventListener('input', handleEmailValidation);
|
---|
| 74 | emailField.addEventListener('focus', () => handleFocus(emailLabel));
|
---|
| 75 | emailField.addEventListener('blur', () => handleBlur(emailField, emailLabel));
|
---|
| 76 |
|
---|
| 77 | passwordFields.forEach((field, index) => {
|
---|
| 78 | const passwordLabel = passwordLabels[index];
|
---|
| 79 | field.addEventListener('focus', () => handleFocus(passwordLabel));
|
---|
| 80 | field.addEventListener('blur', () => handleBlur(field, passwordLabel));
|
---|
| 81 | });
|
---|
| 82 |
|
---|
| 83 | return () => {
|
---|
| 84 | emailField.removeEventListener('input', handleEmailValidation);
|
---|
| 85 | emailField.removeEventListener('focus', () => handleFocus(emailLabel));
|
---|
| 86 | emailField.removeEventListener('blur', () => handleBlur(emailField, emailLabel));
|
---|
| 87 |
|
---|
| 88 | passwordFields.forEach((field, index) => {
|
---|
| 89 | const passwordLabel = passwordLabels[index];
|
---|
| 90 | field.removeEventListener('focus', () => handleFocus(passwordLabel));
|
---|
| 91 | field.removeEventListener('blur', () => handleBlur(field, passwordLabel));
|
---|
| 92 | });
|
---|
| 93 | };
|
---|
| 94 | }, []);
|
---|
| 95 |
|
---|
| 96 | const togglePasswordVisibility = () => {
|
---|
| 97 | setShowPassword(!showPassword);
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | const handleEmailChange = (e) => {
|
---|
| 101 | setEmail(e.target.value);
|
---|
| 102 | };
|
---|
| 103 |
|
---|
| 104 | const handlePasswordChange = (e) => {
|
---|
| 105 | setPassword(e.target.value);
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | const handleLogin = async (e) => {
|
---|
| 109 | e.preventDefault();
|
---|
| 110 | setIsLoading(true);
|
---|
| 111 | setErrorMessage('');
|
---|
| 112 |
|
---|
| 113 | try {
|
---|
| 114 | const response = await fetch('http://localhost:8080/api/login', {
|
---|
| 115 | method: 'POST',
|
---|
| 116 | headers: {
|
---|
| 117 | 'Content-Type': 'application/json',
|
---|
| 118 | },
|
---|
| 119 | body: JSON.stringify({ email, password }),
|
---|
| 120 | });
|
---|
| 121 |
|
---|
| 122 | if (response.ok) {
|
---|
| 123 | const data = await response.json();
|
---|
| 124 | const { token, user_name, user_surname, email, phone_number, address } = data;
|
---|
| 125 |
|
---|
| 126 | localStorage.setItem('token', token);
|
---|
| 127 | localStorage.setItem('userName', user_name);
|
---|
| 128 | localStorage.setItem('userSurname', user_surname || '');
|
---|
| 129 | localStorage.setItem('email', email);
|
---|
| 130 | localStorage.setItem('phoneNumber', phone_number || '');
|
---|
| 131 | localStorage.setItem('address', address || '');
|
---|
| 132 |
|
---|
| 133 | navigate('/');
|
---|
| 134 | } else {
|
---|
| 135 | setErrorMessage('Invalid email or password. Please try again.');
|
---|
| 136 | }
|
---|
| 137 | } catch (error) {
|
---|
| 138 | console.error('Error during login:', error);
|
---|
| 139 | setErrorMessage('An error occurred during login. Please try again.');
|
---|
| 140 | } finally {
|
---|
| 141 | setIsLoading(false);
|
---|
| 142 | }
|
---|
| 143 | };
|
---|
| 144 |
|
---|
| 145 | const handleGoogleLogin = () => {
|
---|
| 146 | window.location.href = 'http://localhost:8080/oauth2/authorization/google';
|
---|
| 147 | };
|
---|
| 148 |
|
---|
| 149 | return (
|
---|
| 150 | <div className={styles.loginContainer}>
|
---|
| 151 | <div className={styles.backgroundContainer}></div>
|
---|
| 152 | <div className={styles.overlay}>
|
---|
| 153 | <form className={styles.form} onSubmit={handleLogin} id="login-form">
|
---|
| 154 | <img src={logo} alt="Logo" className={styles.logo} />
|
---|
| 155 | <p className={styles.title}>Log in to see more</p>
|
---|
| 156 | {errorMessage && <p className={styles.error}>{errorMessage}</p>}
|
---|
| 157 | <label>
|
---|
| 158 | <input
|
---|
| 159 | className={`${styles.input} ${styles.emailInput}`}
|
---|
| 160 | type="email"
|
---|
| 161 | value={email}
|
---|
| 162 | onChange={handleEmailChange}
|
---|
| 163 | required
|
---|
| 164 | />
|
---|
| 165 | <span>Email</span>
|
---|
| 166 | </label>
|
---|
| 167 | <label>
|
---|
| 168 | <input
|
---|
| 169 | className={`${styles.input} ${styles.passwordInput}`}
|
---|
| 170 | type={showPassword ? 'text' : 'password'}
|
---|
| 171 | value={password}
|
---|
| 172 | onChange={handlePasswordChange}
|
---|
| 173 | required
|
---|
| 174 | />
|
---|
| 175 | <span>Password</span>
|
---|
| 176 | <span className={styles.eye} onClick={togglePasswordVisibility}>
|
---|
| 177 | <FontAwesomeIcon icon={showPassword ? faEyeSlash : faEye} id="show-password" />
|
---|
| 178 | </span>
|
---|
| 179 | </label>
|
---|
| 180 | <button type="submit" className={styles.submit} disabled={isLoading}>
|
---|
| 181 | {isLoading ? 'Logging in...' : 'Log in'}
|
---|
| 182 | </button>
|
---|
| 183 | <p className={styles.signin}>
|
---|
| 184 | Don't have an account? <Link to="/Register">Sign up here</Link>
|
---|
| 185 | </p>
|
---|
| 186 | <div className={styles.divider}>
|
---|
| 187 | <span>OR</span>
|
---|
| 188 | </div>
|
---|
| 189 | <button type="button" className={styles.googleButton} onClick={handleGoogleLogin}>
|
---|
| 190 | Continue with Google
|
---|
| 191 | </button>
|
---|
| 192 | </form>
|
---|
| 193 | </div>
|
---|
| 194 | </div>
|
---|
| 195 | );
|
---|
| 196 | };
|
---|
| 197 |
|
---|
| 198 | export default Login;
|
---|