mirror of
https://github.com/R0m1k3/FlowReader.git
synced 2026-10-11 17:28:05 +02:00
85 lines
3.0 KiB
TypeScript
85 lines
3.0 KiB
TypeScript
import { useState } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { useMutation } from '@tanstack/react-query';
|
|
import { authApi, ApiError } from '../api/auth';
|
|
import { useAuthStore } from '../stores/authStore';
|
|
|
|
export function LoginPage() {
|
|
const navigate = useNavigate();
|
|
const setUser = useAuthStore((state) => state.setUser);
|
|
const [email, setEmail] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState('');
|
|
|
|
const loginMutation = useMutation({
|
|
mutationFn: authApi.login,
|
|
onSuccess: (data) => {
|
|
setUser(data.user);
|
|
navigate('/');
|
|
},
|
|
onError: (err: Error) => {
|
|
if (err instanceof ApiError) {
|
|
setError(err.message);
|
|
} else {
|
|
setError('Login failed. Please try again.');
|
|
}
|
|
},
|
|
});
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setError('');
|
|
loginMutation.mutate({ email, password });
|
|
};
|
|
|
|
return (
|
|
<div className="login-container">
|
|
<div className="login-card">
|
|
<h1>FlowReader</h1>
|
|
<p className="subtitle">Connectez-vous à votre compte</p>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
{error && <div className="error-message">{error}</div>}
|
|
|
|
<div className="form-group">
|
|
<label htmlFor="email">Email</label>
|
|
<input
|
|
id="email"
|
|
type="email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
placeholder="vous@example.com"
|
|
required
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
|
|
<div className="form-group">
|
|
<label htmlFor="password">Mot de passe</label>
|
|
<input
|
|
id="password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
placeholder="••••••••"
|
|
required
|
|
minLength={8}
|
|
/>
|
|
</div>
|
|
|
|
<button type="submit" disabled={loginMutation.isPending}>
|
|
{loginMutation.isPending ? 'Connexion...' : 'Se connecter'}
|
|
</button>
|
|
</form>
|
|
|
|
<p className="register-link">
|
|
Pas encore de compte ?{' '}
|
|
<a href="/register" onClick={(e) => { e.preventDefault(); navigate('/register'); }}>
|
|
S'inscrire
|
|
</a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|