|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import photo from '../../../assets/login.png'; |
| 3 | +import { Link, useNavigate } from 'react-router-dom'; |
| 4 | +import { message } from 'antd'; |
| 5 | +import Cookies from 'js-cookie'; |
| 6 | +import { FaEye } from 'react-icons/fa'; |
| 7 | +import { FaEyeSlash } from 'react-icons/fa6'; |
| 8 | +import { useUser } from '../../../context/userContext'; |
| 9 | + |
| 10 | +const AdminLogin = () => { |
| 11 | + const API_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000'; |
| 12 | + const [data, setData] = useState({ |
| 13 | + email: '', |
| 14 | + password: '', |
| 15 | + }); |
| 16 | + const [hidden, setHidden] = useState(true); |
| 17 | + const [isLoading, setIsLoading] = useState(false); |
| 18 | + const [error, setError] = useState(null); |
| 19 | + const {user, setUser} = useUser(); |
| 20 | + |
| 21 | + const navigate = useNavigate(); |
| 22 | + |
| 23 | + const handleChange = (e) => { |
| 24 | + setData({ ...data, [e.target.name]: e.target.value }); |
| 25 | + }; |
| 26 | + |
| 27 | + const handleSubmit = async (e) => { |
| 28 | + e.preventDefault(); |
| 29 | + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 30 | + if (!emailRegex.test(data.email)) { |
| 31 | + setError('Please enter a valid email address'); |
| 32 | + return; |
| 33 | + } |
| 34 | + if (data.password.length < 8) { |
| 35 | + setError('Password must be at least 8 characters long'); |
| 36 | + return; |
| 37 | + } |
| 38 | + setIsLoading(true); |
| 39 | + setError(null); |
| 40 | + try { |
| 41 | + const response = await fetch(`${API_URL}/api/admin/login`, { |
| 42 | + method: 'POST', |
| 43 | + headers: { |
| 44 | + 'Content-Type': 'application/json', |
| 45 | + }, |
| 46 | + body: JSON.stringify(data), |
| 47 | + }); |
| 48 | + const result = await response.json(); |
| 49 | + if (!response.ok) { |
| 50 | + throw new Error(result.message || 'Login failed'); |
| 51 | + } |
| 52 | + const res = JSON.stringify(result.admin) |
| 53 | + Cookies.set('authToken', result.token, { expires: 1, secure: true }); |
| 54 | + Cookies.set("authenticatedUser", res, {expires: 1, secure: true}) |
| 55 | + setUser(result.admin) |
| 56 | + message.success('Login successful'); |
| 57 | + navigate('/admin'); |
| 58 | + } catch (err) { |
| 59 | + setError(err.message || 'An error occurred. Please try again.'); |
| 60 | + } finally { |
| 61 | + setIsLoading(false); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + window.scrollTo(0, 0); |
| 67 | + }, []); |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className="w-screen h-screen dark:bg-black flex items-center justify-center lg:pt-10 px-4"> |
| 71 | + {/* Background Image */} |
| 72 | + <img |
| 73 | + src={photo} |
| 74 | + alt="login" |
| 75 | + loading="lazy" |
| 76 | + className="absolute w-3/4 lg:w-auto lg:opacity-100 opacity-10 object-cover" |
| 77 | + /> |
| 78 | + {/* Login Form */} |
| 79 | + <form |
| 80 | + onSubmit={handleSubmit} |
| 81 | + className="z-10 p-8 lg:p-14 bg-[#f1e9dc] dark:bg-amber-800 dark:text-white flex flex-col gap-6 rounded-lg border-2 border-black shadow-[4px_4px_0px_0px_black] w-full max-w-md lg:max-w-xl" |
| 82 | + > |
| 83 | + <div className="text-[#323232] dark:text-white font-black text-4xl lg:text-6xl mb-2"> |
| 84 | + Admin Login, |
| 85 | + <span className="block text-[#666] dark:text-gray-400 font-semibold text-lg lg:text-2xl mt-1"> |
| 86 | + Log in to continue |
| 87 | + </span> |
| 88 | + </div> |
| 89 | + |
| 90 | + <input |
| 91 | + className="w-full h-12 rounded-md border-2 border-black bg-beige shadow-[4px_4px_0px_0px_black] text-[15px] font-semibold text-[#323232] p-2.5 focus:outline-none focus:border-[#2d8cf0] placeholder-[#666]" |
| 92 | + name="email" |
| 93 | + placeholder="Email" |
| 94 | + type="email" |
| 95 | + onChange={handleChange} |
| 96 | + /> |
| 97 | + |
| 98 | + <div className="relative w-full"> |
| 99 | + <input |
| 100 | + className="w-full h-12 rounded-md border-2 border-black bg-beige shadow-[4px_4px_0px_0px_black] text-[15px] font-semibold text-[#323232] p-2.5 focus:outline-none focus:border-[#2d8cf0] placeholder-[#666]" |
| 101 | + name="password" |
| 102 | + placeholder="Password" |
| 103 | + type={hidden ? 'password' : 'text'} |
| 104 | + onChange={handleChange} |
| 105 | + /> |
| 106 | + <button |
| 107 | + className="absolute top-1/2 transform -translate-y-1/2 right-4" |
| 108 | + onClick={(e) => { |
| 109 | + e.preventDefault(); |
| 110 | + setHidden(!hidden); |
| 111 | + }} |
| 112 | + > |
| 113 | + {hidden ? <FaEyeSlash /> : <FaEye />} |
| 114 | + </button> |
| 115 | + </div> |
| 116 | + |
| 117 | + <Link |
| 118 | + to="/email-verify" |
| 119 | + className="text-sm lg:text-base text-gray-500 dark:text-gray-200 hover:text-red-500 transition" |
| 120 | + > |
| 121 | + Forgot Password? |
| 122 | + </Link> |
| 123 | + |
| 124 | + <h3 className="flex justify-between items-center w-full text-sm lg:text-base"> |
| 125 | + Don’t have an account? |
| 126 | + <Link |
| 127 | + to="/admin-signup" |
| 128 | + className="text-green-500 font-semibold hover:scale-110 transition" |
| 129 | + > |
| 130 | + Register Here |
| 131 | + </Link> |
| 132 | + </h3> |
| 133 | + |
| 134 | + <Link |
| 135 | + to={`${API_URL}/api/user/auth/google`} |
| 136 | + className="w-full" |
| 137 | + > |
| 138 | + <button |
| 139 | + type="button" |
| 140 | + className="w-full h-12 rounded-md border-2 dark:text-white border-black bg-beige shadow-[4px_4px_0px_0px_black] text-[17px] font-semibold text-[#323232] transition active:translate-x-[3px] active:translate-y-[3px]" |
| 141 | + > |
| 142 | + Sign in with Google |
| 143 | + </button> |
| 144 | + </Link> |
| 145 | + |
| 146 | + {error && <p className="text-red-500 mt-2">{error}</p>} |
| 147 | + |
| 148 | + <button |
| 149 | + type="submit" |
| 150 | + className="w-full h-12 rounded-md dark:text-white border-2 border-black bg-beige shadow-[4px_4px_0px_0px_black] text-[17px] font-semibold text-[#323232] transition active:translate-x-[3px] active:translate-y-[3px]" |
| 151 | + > |
| 152 | + {isLoading ? 'Loading...' : 'Let’s Log you in →'} |
| 153 | + </button> |
| 154 | + </form> |
| 155 | + </div> |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +export default AdminLogin; |
0 commit comments