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