Spaces:
Runtime error
Runtime error
| import React, { useState } from 'react'; | |
| import { useAuth } from '../../context/AuthContext'; | |
| import { useNavigate } from 'react-router-dom'; | |
| const LoginForm = () => { | |
| const [email, setEmail] = useState(''); | |
| const [password, setPassword] = useState(''); | |
| const [error, setError] = useState(''); | |
| const { login } = useAuth(); | |
| const navigate = useNavigate(); | |
| const handleSubmit = async (e) => { | |
| e.preventDefault(); | |
| try { | |
| await login(email, password); | |
| navigate('/dashboard'); | |
| } catch (error) { | |
| setError('Invalid credentials'); | |
| } | |
| }; | |
| return ( | |
| <form onSubmit={handleSubmit} className="space-y-4"> | |
| <div> | |
| <label className="block text-sm font-medium text-gray-700">Email</label> | |
| <input | |
| type="email" | |
| value={email} | |
| onChange={(e) => setEmail(e.target.value)} | |
| className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500" | |
| required | |
| /> | |
| </div> | |
| <div> | |
| <label className="block text-sm font-medium text-gray-700">Password</label> | |
| <input | |
| type="password" | |
| value={password} | |
| onChange={(e) => setPassword(e.target.value)} | |
| className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500" | |
| required | |
| /> | |
| </div> | |
| {error && <p className="text-red-500 text-sm">{error}</p>} | |
| <button | |
| type="submit" | |
| className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" | |
| > | |
| Sign In | |
| </button> | |
| </form> | |
| ); | |
| }; | |
| export default LoginForm; |