feat(auth): LoginPage - fix credentials, fetch /auth/me after login
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { useState } from 'react';
|
||||
import { setToken, setSessionUser } from '../lib/session';
|
||||
|
||||
type Props = { onLogin: (token: string) => void };
|
||||
type Props = { onLogin: () => void };
|
||||
|
||||
export function LoginPage({ onLogin }: Props) {
|
||||
const [username, setUsername] = useState('admin');
|
||||
const [password, setPassword] = useState('admin123');
|
||||
const [password, setPassword] = useState('admin');
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
@@ -16,7 +17,7 @@ export function LoginPage({ onLogin }: Props) {
|
||||
const body = new URLSearchParams();
|
||||
body.set('username', username);
|
||||
body.set('password', password);
|
||||
const res = await fetch('/auth/token', {
|
||||
const res = await fetch('/backend/auth/token', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: body.toString(),
|
||||
@@ -25,8 +26,13 @@ export function LoginPage({ onLogin }: Props) {
|
||||
const data = await res.json().catch(() => null);
|
||||
throw new Error(data?.detail ?? 'Login fallito');
|
||||
}
|
||||
const data = await res.json();
|
||||
onLogin(data.access_token);
|
||||
const { access_token } = await res.json();
|
||||
setToken(access_token);
|
||||
const meRes = await fetch('/backend/auth/me', {
|
||||
headers: { Authorization: `Bearer ${access_token}` },
|
||||
});
|
||||
if (meRes.ok) setSessionUser(await meRes.json());
|
||||
onLogin();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Login fallito');
|
||||
} finally {
|
||||
@@ -49,7 +55,6 @@ export function LoginPage({ onLogin }: Props) {
|
||||
Password
|
||||
<input type="password" value={password} onChange={(e) => setPassword(e.target.value)} autoComplete="current-password" />
|
||||
</label>
|
||||
<p className="login-hint">Credenziali demo: admin / admin123 oppure viewer / viewer123</p>
|
||||
{error && <p className="login-error">{error}</p>}
|
||||
<button type="submit" disabled={loading}>{loading ? 'Accesso...' : 'Entra'}</button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user