|
| 1 | +import React, { useState, ChangeEvent, FormEvent } from "react"; |
| 2 | +import axios from "axios"; |
| 3 | +import { useNavigate, Link } from "react-router-dom"; // Import the hook for navigation |
| 4 | +import GithubIcon from "@mui/icons-material/GitHub"; |
| 5 | + |
| 6 | +const backendUrl = import.meta.env.VITE_BACKEND_URL; |
| 7 | +interface LoginFormData { |
| 8 | + email: string; |
| 9 | + password: string; |
| 10 | +} |
| 11 | + |
| 12 | +const Login: React.FC = () => { |
| 13 | + const [formData, setFormData] = useState<LoginFormData>({ email: "", password: "" }); |
| 14 | + const [message, setMessage] = useState<string>(""); |
| 15 | + |
| 16 | + const navigate = useNavigate(); // Initialize the navigate hook |
| 17 | + |
| 18 | + const handleChange = (e: ChangeEvent<HTMLInputElement>) => { |
| 19 | + const { name, value } = e.target; |
| 20 | + setFormData({ ...formData, [name]: value }); |
| 21 | + }; |
| 22 | + |
| 23 | + const handleSubmit = async (e: FormEvent) => { |
| 24 | + e.preventDefault(); |
| 25 | + try { |
| 26 | + const response = await axios.post(`${backendUrl}/api/auth/login`, |
| 27 | + formData, |
| 28 | + |
| 29 | + ); |
| 30 | + setMessage(response.data.message); // Show success message from backend |
| 31 | + |
| 32 | + // Navigate to /home if login is successful |
| 33 | + if (response.data.message === 'Login successful') { |
| 34 | + navigate("/home"); |
| 35 | + } |
| 36 | + } catch (error: any) { |
| 37 | + setMessage(error.response?.data?.message || "Something went wrong"); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const handleGithubLogin = async () => { |
| 42 | + window.location.href = `${import.meta.env.VITE_API_URL}/auth/github`; |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="w-[400px] max-w-screen-xl mx-auto bg-white p-8 rounded-lg shadow-md"> |
| 47 | + <h2 className="text-2xl font-semibold text-center mb-6">Login</h2> |
| 48 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 49 | + <div> |
| 50 | + <input |
| 51 | + type="email" |
| 52 | + name="email" |
| 53 | + placeholder="Email" |
| 54 | + value={formData.email} |
| 55 | + onChange={handleChange} |
| 56 | + required |
| 57 | + className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 58 | + /> |
| 59 | + </div> |
| 60 | + <div> |
| 61 | + <input |
| 62 | + type="password" |
| 63 | + name="password" |
| 64 | + placeholder="Password" |
| 65 | + value={formData.password} |
| 66 | + onChange={handleChange} |
| 67 | + required |
| 68 | + className="w-full p-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 69 | + /> |
| 70 | + </div> |
| 71 | + <button |
| 72 | + type="submit" |
| 73 | + className="w-full bg-blue-500 text-white py-3 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 74 | + > |
| 75 | + Login |
| 76 | + </button> |
| 77 | + </form> |
| 78 | + <button |
| 79 | + onClick={handleGithubLogin} |
| 80 | + className="w-full flex items-center justify-center bg-gray-800 text-white py-3 rounded-md hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-gray-500 my-4" |
| 81 | + > |
| 82 | + <GithubIcon className="mr-4"/>Login with GitHub |
| 83 | + </button> |
| 84 | + <div className="text-center"> |
| 85 | + <p className="text-sm text-gray-600"> |
| 86 | + Don't have an account?{" "} |
| 87 | + <Link to="/signup" className="text-blue-500 hover:text-blue-600"> |
| 88 | + Sign up |
| 89 | + </Link> |
| 90 | + </p> |
| 91 | + </div> |
| 92 | + {message && <p className="text-center text-red-500 mt-4">{message}</p>} |
| 93 | + </div> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default Login; |
| 98 | + |
0 commit comments