diff --git a/.gitignore b/.gitignore index 8087f39..71f400b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ eggs/ .eggs/ lib/ lib64/ +# Allow frontend source lib directories +!admin-ui/src/lib/ parts/ sdist/ var/ diff --git a/admin-ui/src/App.tsx b/admin-ui/src/App.tsx index 2b14576..55bfedf 100644 --- a/admin-ui/src/App.tsx +++ b/admin-ui/src/App.tsx @@ -1,21 +1,34 @@ -import { BrowserRouter, Routes, Route } from 'react-router-dom' +import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom' +import { AuthProvider, useAuth } from './auth' import Layout from './components/Layout' import Dashboard from './pages/Dashboard' import Accounts from './pages/Accounts' import Emails from './pages/Emails' import ConnectionTest from './pages/ConnectionTest' +import Login from './pages/Login' + +function AuthedRoutes() { + const { credentials } = useAuth() + if (!credentials) return + return ( + + }> + } /> + } /> + } /> + } /> + } /> + + + ) +} export default function App() { return ( - - - }> - } /> - } /> - } /> - } /> - - - + + + + + ) } diff --git a/admin-ui/src/auth.tsx b/admin-ui/src/auth.tsx new file mode 100644 index 0000000..bba6ab9 --- /dev/null +++ b/admin-ui/src/auth.tsx @@ -0,0 +1,40 @@ +import { createContext, useContext, useState, ReactNode } from 'react' + +interface AuthCtx { + credentials: string | null + login: (user: string, pass: string) => void + logout: () => void +} + +const Ctx = createContext({ + credentials: null, + login: () => {}, + logout: () => {}, +}) + +const KEY = 'fwd_auth' + +function stored(): string | null { + try { return sessionStorage.getItem(KEY) } catch { return null } +} + +export function AuthProvider({ children }: { children: ReactNode }) { + const [credentials, setCredentials] = useState(stored) + + const login = (user: string, pass: string) => { + const encoded = btoa(`${user}:${pass}`) + sessionStorage.setItem(KEY, encoded) + setCredentials(encoded) + } + + const logout = () => { + sessionStorage.removeItem(KEY) + setCredentials(null) + } + + return {children} +} + +export function useAuth() { + return useContext(Ctx) +} diff --git a/admin-ui/src/components/Layout.tsx b/admin-ui/src/components/Layout.tsx index 9e49b5e..a4ff433 100644 --- a/admin-ui/src/components/Layout.tsx +++ b/admin-ui/src/components/Layout.tsx @@ -1,5 +1,6 @@ import { NavLink, Outlet } from 'react-router-dom' -import { LayoutDashboard, Users, Mail, Wifi } from 'lucide-react' +import { LayoutDashboard, Users, Mail, Wifi, LogOut } from 'lucide-react' +import { useAuth } from '../auth' const nav = [ { to: '/', label: 'Dashboard', icon: LayoutDashboard }, @@ -9,6 +10,8 @@ const nav = [ ] export default function Layout() { + const { logout } = useAuth() + return ( diff --git a/admin-ui/src/lib/api.ts b/admin-ui/src/lib/api.ts index dcfb0b7..2976960 100644 --- a/admin-ui/src/lib/api.ts +++ b/admin-ui/src/lib/api.ts @@ -1,8 +1,16 @@ const BASE = '/api' +function authHeader(): Record { + try { + const cred = sessionStorage.getItem('fwd_auth') + if (cred) return { Authorization: `Basic ${cred}` } + } catch { /* ignore */ } + return {} +} + async function request(path: string, init?: RequestInit): Promise { const res = await fetch(`${BASE}${path}`, { - headers: { 'Content-Type': 'application/json', ...init?.headers }, + headers: { 'Content-Type': 'application/json', ...authHeader(), ...init?.headers }, ...init, }) if (!res.ok) { diff --git a/admin-ui/src/pages/Login.tsx b/admin-ui/src/pages/Login.tsx new file mode 100644 index 0000000..bd044d5 --- /dev/null +++ b/admin-ui/src/pages/Login.tsx @@ -0,0 +1,78 @@ +import { useState, FormEvent } from 'react' +import { useAuth } from '../auth' + +export default function Login() { + const { login } = useAuth() + const [user, setUser] = useState('') + const [pass, setPass] = useState('') + const [error, setError] = useState(null) + const [loading, setLoading] = useState(false) + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault() + setError(null) + setLoading(true) + try { + const encoded = btoa(`${user}:${pass}`) + const res = await fetch('/api/stats', { + headers: { Authorization: `Basic ${encoded}` }, + }) + if (res.status === 401) { + setError('Credenciais inválidas') + return + } + login(user, pass) + } catch { + setError('Erro de ligação ao servidor') + } finally { + setLoading(false) + } + } + + return ( + + + BizDocs Forwarder + Iniciar sessão + + + + Utilizador + setUser(e.target.value)} + autoFocus + required + className="w-full border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500" + /> + + + Password + setPass(e.target.value)} + required + className="w-full border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500" + /> + + + {error && ( + + {error} + + )} + + + {loading ? 'A autenticar…' : 'Entrar'} + + + + + ) +}
Iniciar sessão
+ {error} +