feat(admin-ui): add login page and HTTP Basic auth
- AuthProvider + useAuth hook (sessionStorage, base64 creds) - Login.tsx validates credentials against /api/stats before accepting - api.ts injects Authorization: Basic header on all requests - App.tsx gates all routes behind login, redirects unknown paths - Layout.tsx adds logout button - .gitignore: allow admin-ui/src/lib/ (was blocked by root lib/ rule) Co-Authored-By: Paperclip <[email protected]>
This commit is contained in:
1 parent
6e6f1a44e4
commit
d476cb0204
6 files changed
+166
-13
No files matched your search
@@ -12,6 +12,8 @@ eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
# Allow frontend source lib directories
|
||||
!admin-ui/src/lib/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
|
||||
+24
-11
@@ -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 <Login />
|
||||
return (
|
||||
<Routes>
|
||||
<Route element={<Layout />}>
|
||||
<Route index element={<Dashboard />} />
|
||||
<Route path="/accounts" element={<Accounts />} />
|
||||
<Route path="/emails" element={<Emails />} />
|
||||
<Route path="/connection-test" element={<ConnectionTest />} />
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
)
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route element={<Layout />}>
|
||||
<Route index element={<Dashboard />} />
|
||||
<Route path="/accounts" element={<Accounts />} />
|
||||
<Route path="/emails" element={<Emails />} />
|
||||
<Route path="/connection-test" element={<ConnectionTest />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
<AuthProvider>
|
||||
<BrowserRouter>
|
||||
<AuthedRoutes />
|
||||
</BrowserRouter>
|
||||
</AuthProvider>
|
||||
)
|
||||
}
|
||||
@@ -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<AuthCtx>({
|
||||
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<string | null>(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 <Ctx.Provider value={{ credentials, login, logout }}>{children}</Ctx.Provider>
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
return useContext(Ctx)
|
||||
}
|
||||
@@ -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 (
|
||||
<div className="flex h-screen">
|
||||
<aside className="w-56 bg-gray-900 text-white flex flex-col">
|
||||
@@ -35,6 +38,15 @@ export default function Layout() {
|
||||
</NavLink>
|
||||
))}
|
||||
</nav>
|
||||
<div className="p-3 border-t border-gray-700">
|
||||
<button
|
||||
onClick={logout}
|
||||
className="flex items-center gap-3 px-3 py-2 w-full rounded-md text-sm text-gray-400 hover:bg-gray-800 hover:text-white transition-colors"
|
||||
>
|
||||
<LogOut size={16} />
|
||||
Sair
|
||||
</button>
|
||||
</div>
|
||||
</aside>
|
||||
<main className="flex-1 overflow-auto">
|
||||
<Outlet />
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
const BASE = '/api'
|
||||
|
||||
function authHeader(): Record<string, string> {
|
||||
try {
|
||||
const cred = sessionStorage.getItem('fwd_auth')
|
||||
if (cred) return { Authorization: `Basic ${cred}` }
|
||||
} catch { /* ignore */ }
|
||||
return {}
|
||||
}
|
||||
|
||||
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
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) {
|
||||
|
||||
@@ -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<string | null>(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 (
|
||||
<div className="min-h-screen bg-gray-50 flex items-center justify-center">
|
||||
<div className="bg-white rounded-lg border border-gray-200 p-8 w-full max-w-sm shadow-sm">
|
||||
<h1 className="text-xl font-semibold text-gray-900 mb-1">BizDocs Forwarder</h1>
|
||||
<p className="text-sm text-gray-500 mb-6">Iniciar sessão</p>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-gray-700 mb-1">Utilizador</label>
|
||||
<input
|
||||
type="text"
|
||||
value={user}
|
||||
onChange={e => 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"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-gray-700 mb-1">Password</label>
|
||||
<input
|
||||
type="password"
|
||||
value={pass}
|
||||
onChange={e => 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"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="text-xs text-red-600 bg-red-50 border border-red-200 rounded px-3 py-2">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full bg-indigo-600 text-white text-sm py-2 rounded-md hover:bg-indigo-700 disabled:opacity-50"
|
||||
>
|
||||
{loading ? 'A autenticar…' : 'Entrar'}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user