| | import React, { useState } from 'react'; |
| | import Link from '@docusaurus/Link'; |
| | import { useHistory } from '@docusaurus/router'; |
| | import apiService from '../../utils/apiService'; |
| | import './Auth.css'; |
| |
|
| | const Login = () => { |
| | const [formData, setFormData] = useState({ |
| | email: '', |
| | password: '' |
| | }); |
| | const [error, setError] = useState(''); |
| | const [loading, setLoading] = useState(false); |
| |
|
| | const history = useHistory(); |
| |
|
| | const handleChange = (e) => { |
| | setFormData({ |
| | ...formData, |
| | [e.target.name]: e.target.value |
| | }); |
| | }; |
| |
|
| | const handleSubmit = async (e) => { |
| | e.preventDefault(); |
| | setLoading(true); |
| | setError(''); |
| |
|
| | try { |
| | |
| | const response = await apiService.auth.login(formData); |
| |
|
| | |
| | localStorage.setItem('access_token', response.access_token); |
| | history.push('/'); |
| | } catch (err) { |
| | setError(err.message || 'Login failed. Please check your credentials and try again.'); |
| | } finally { |
| | setLoading(false); |
| | } |
| | }; |
| |
|
| | return ( |
| | <div className="auth-container"> |
| | <div className="auth-form"> |
| | <h2>Login to Your Account</h2> |
| | {error && <div className="error-message">{error}</div>} |
| | <form onSubmit={handleSubmit}> |
| | <div className="form-group"> |
| | <label htmlFor="email">Email</label> |
| | <input |
| | type="email" |
| | id="email" |
| | name="email" |
| | value={formData.email} |
| | onChange={handleChange} |
| | required |
| | /> |
| | </div> |
| | <div className="form-group"> |
| | <label htmlFor="password">Password</label> |
| | <input |
| | type="password" |
| | id="password" |
| | name="password" |
| | value={formData.password} |
| | onChange={handleChange} |
| | required |
| | /> |
| | </div> |
| | <button type="submit" disabled={loading} className="auth-button"> |
| | {loading ? 'Logging in...' : 'Login'} |
| | </button> |
| | </form> |
| | <div className="auth-footer"> |
| | <p> |
| | Don't have an account? <Link to="/signup">Sign up</Link> |
| | </p> |
| | <p> |
| | <Link to="/">Back to home</Link> |
| | </p> |
| | </div> |
| | </div> |
| | </div> |
| | ); |
| | }; |
| |
|
| | export default Login; |