|
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import photo from '../../../assets/login.png'; |
| 3 | +import { useNavigate, Link } from 'react-router-dom'; |
| 4 | +import { FaEye } from 'react-icons/fa'; |
| 5 | +import { FaEyeSlash } from 'react-icons/fa6'; |
| 6 | +import zxcvbn from 'zxcvbn'; // Password strength checker |
| 7 | + |
| 8 | +const AdminSignup = () => { |
| 9 | + const API_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000'; |
| 10 | + const navigate = useNavigate(); |
| 11 | + const [isLoading, setIsLoading] = useState(false); |
| 12 | + const [error, setError] = useState(null); |
| 13 | + const [passwordStrength, setPasswordStrength] = useState(0); |
| 14 | + const [data, setData] = useState({ name: '', email: '', password: '' }); |
| 15 | + const [hidden, setHidden] = useState(true); |
| 16 | + |
| 17 | + const handleChange = (e) => { |
| 18 | + setData({ ...data, [e.target.name]: e.target.value }); |
| 19 | + if (e.target.name === 'password') { |
| 20 | + const result = zxcvbn(e.target.value); |
| 21 | + setPasswordStrength(result.score); |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + const handleSubmit = async (e) => { |
| 26 | + e.preventDefault(); |
| 27 | + setIsLoading(true); |
| 28 | + |
| 29 | + // Input validation |
| 30 | + if (!data.email || !data.password || !data.name) { |
| 31 | + setError('Please fill in all fields'); |
| 32 | + setIsLoading(false); |
| 33 | + return; |
| 34 | + } |
| 35 | + if (data.password.length < 8) { |
| 36 | + setError('Password must be at least 8 characters long'); |
| 37 | + setIsLoading(false); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (data.name.length < 3) { |
| 41 | + setError('Name must be at least 3 characters long'); |
| 42 | + setIsLoading(false); |
| 43 | + return; |
| 44 | + } |
| 45 | + if (!data.email.includes('@')) { |
| 46 | + setError('Please enter a valid email address'); |
| 47 | + setIsLoading(false); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + const response = await fetch(`${API_URL}/api/admin/register`, { |
| 53 | + method: 'POST', |
| 54 | + headers: { 'Content-Type': 'application/json' }, |
| 55 | + body: JSON.stringify(data), |
| 56 | + }); |
| 57 | + const result = await response.json(); |
| 58 | + |
| 59 | + if (!response.ok) { |
| 60 | + setIsLoading(false); |
| 61 | + setError(result.error); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + // alert('OTP sent to your email. Verify to complete registration.'); |
| 67 | + navigate('/admin-login'); |
| 68 | + |
| 69 | + } catch (error) { |
| 70 | + setError(error.message); |
| 71 | + console.error('Error:', error); |
| 72 | + } finally { |
| 73 | + setIsLoading(false); // Ensure loading state is reset after request |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + window.scrollTo(0, 0); |
| 80 | + }, []); |
| 81 | + |
| 82 | + const getPasswordStrengthColor = (score) => { |
| 83 | + const colors = ['#ff4d4d', '#ff944d', '#ffd24d', '#d2ff4d', '#4dff88']; |
| 84 | + return colors[score]; |
| 85 | + }; |
| 86 | + |
| 87 | + const getPasswordStrengthText = (score) => { |
| 88 | + const strengthLevels = ['Very Weak', 'Weak', 'Okay', 'Good', 'Strong']; |
| 89 | + return strengthLevels[score]; |
| 90 | + }; |
| 91 | + |
| 92 | + return ( |
| 93 | + <div className="w-screen h-screen flex items-center dark:text-white dark:bg-black justify-center pt-10 relative overflow-hidden"> |
| 94 | + <img |
| 95 | + src={photo} |
| 96 | + alt="login" |
| 97 | + loading="lazy" |
| 98 | + className="absolute w-3/4 lg:w-auto lg:opacity-100 opacity-10 object-cover" |
| 99 | + /> |
| 100 | + <form className="z-10 p-8 sm:p-16 bg-[#f1e9dc] dark:bg-amber-800 dark:text-white rounded-lg border-2 border-black shadow-[4px_4px_0px_0px_black] flex flex-col gap-5 w-11/12 sm:w-auto px-[3vw] pt-[5vh]"> |
| 101 | + <div className="text-[#323232] dark:text-white font-black text-4xl sm:text-7xl mb-4 sm:mb-6 mt-4"> |
| 102 | + Admin SignUp |
| 103 | + <br /> |
| 104 | + <span className="block text-[#666] dark:text-gray-400 font-semibold text-xl sm:text-2xl"> |
| 105 | + Register to continue |
| 106 | + </span> |
| 107 | + </div> |
| 108 | + <input |
| 109 | + className="input w-full h-10 rounded-md border-2 border-black bg-beige p-2.5 shadow-[4px_4px_0px_0px_black] focus:outline-none" |
| 110 | + name="name" |
| 111 | + placeholder="Name" |
| 112 | + type="text" |
| 113 | + onChange={handleChange} |
| 114 | + /> |
| 115 | + <input |
| 116 | + className="input w-full h-10 rounded-md border-2 border-black bg-beige p-2.5 shadow-[4px_4px_0px_0px_black] focus:outline-none" |
| 117 | + name="email" |
| 118 | + placeholder="Email" |
| 119 | + type="email" |
| 120 | + onChange={handleChange} |
| 121 | + /> |
| 122 | + <div className="relative w-full"> |
| 123 | + <input |
| 124 | + className="input w-full h-10 rounded-md border-2 border-black bg-beige p-2.5 shadow-[4px_4px_0px_0px_black] focus:outline-none" |
| 125 | + name="password" |
| 126 | + placeholder="Password" |
| 127 | + type={hidden ? 'password' : 'text'} |
| 128 | + onChange={handleChange} |
| 129 | + /> |
| 130 | + <button |
| 131 | + className="absolute top-1/2 -translate-y-1/2 right-4" |
| 132 | + onClick={(e) => { |
| 133 | + e.preventDefault(); |
| 134 | + setHidden(!hidden); |
| 135 | + }} |
| 136 | + > |
| 137 | + {hidden ? <FaEyeSlash /> : <FaEye />} |
| 138 | + </button> |
| 139 | + </div> |
| 140 | + <div className="w-full mt-2"> |
| 141 | + <div |
| 142 | + className="h-2 rounded-full" |
| 143 | + style={{ |
| 144 | + backgroundColor: getPasswordStrengthColor(passwordStrength), |
| 145 | + width: `${(passwordStrength + 1) * 20}%`, |
| 146 | + }} |
| 147 | + ></div> |
| 148 | + <p className="text-sm text-[#666] dark:text-gray-200 mt-1"> |
| 149 | + Strength: {getPasswordStrengthText(passwordStrength)} |
| 150 | + </p> |
| 151 | + </div> |
| 152 | + {error && ( |
| 153 | + <div className="w-full p-2 bg-red-100 text-red-700 border border-red-400 rounded-md"> |
| 154 | + {error} |
| 155 | + </div> |
| 156 | + )} |
| 157 | + <h3 className="flex justify-between w-full"> |
| 158 | + Already have an account? |
| 159 | + <Link |
| 160 | + to="/admin-login" |
| 161 | + className="text-[#666] dark:text-white font-semibold hover:text-green-500" |
| 162 | + > |
| 163 | + Login |
| 164 | + </Link> |
| 165 | + </h3> |
| 166 | + <a href={`${API_URL}/api/user/auth/google`} className="w-full"> |
| 167 | + <button className="button-confirm w-full h-10 rounded-md border-2 border-black bg-beige text-[17px] font-semibold shadow-[4px_4px_0px_0px_black] hover:text-green-300"> |
| 168 | + Sign up with Google |
| 169 | + </button> |
| 170 | + </a> |
| 171 | + <button |
| 172 | + className="button-confirm w-full h-10 rounded-md border-2 border-black bg-beige text-[17px] font-semibold shadow-[4px_4px_0px_0px_black] mb-2 hover:text-green-300" |
| 173 | + onClick={handleSubmit} |
| 174 | + > |
| 175 | + {isLoading ? 'Loading...' : "Let's go →"} |
| 176 | + </button> |
| 177 | + </form> |
| 178 | + </div> |
| 179 | + ); |
| 180 | +}; |
| 181 | + |
| 182 | +export default AdminSignup; |
0 commit comments