feat: add authentication and book upload
- Session-based login (username/password, 30-day cookie) - SQLite user store with bcrypt password hashing (modernc.org/sqlite) - Per-user upload permission (can_upload flag) - Admin CLI (cmd/admin) for user management: user add/list/delete/set-upload - Upload handler for EPUB/PDF with path-traversal protection - All routes protected by requireAuth middleware; /upload additionally requires requireUpload - Login/logout UI, upload form, logout button in header - New env var: USERS_DB (default: users.db, gitignored) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+58
-1
@@ -21,6 +21,9 @@ templ Layout(title string, backHref string) {
|
||||
<a class="header-back" href={ backHref } aria-label="Zurück">←</a>
|
||||
}
|
||||
<h1><a href="/">eBook Library</a></h1>
|
||||
<form method="post" action="/logout" style="margin-left:auto">
|
||||
<button type="submit" class="btn-logout">Abmelden</button>
|
||||
</form>
|
||||
</div>
|
||||
</header>
|
||||
<main class="container">
|
||||
@@ -30,6 +33,57 @@ templ Layout(title string, backHref string) {
|
||||
</html>
|
||||
}
|
||||
|
||||
templ LoginLayout(title string) {
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>{ title }</title>
|
||||
<link rel="stylesheet" href="/static/styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
<main class="container">
|
||||
{ children... }
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
|
||||
templ LoginPage(errMsg string) {
|
||||
@LoginLayout("Anmelden") {
|
||||
<section class="login-box">
|
||||
<h2>Anmelden</h2>
|
||||
if errMsg != "" {
|
||||
<p class="error">{ errMsg }</p>
|
||||
}
|
||||
<form method="post" action="/login" class="login-form">
|
||||
<label for="username">Benutzername</label>
|
||||
<input id="username" type="text" name="username" required autofocus />
|
||||
<label for="password">Passwort</label>
|
||||
<input id="password" type="password" name="password" required />
|
||||
<button type="submit" class="btn">Anmelden</button>
|
||||
</form>
|
||||
</section>
|
||||
}
|
||||
}
|
||||
|
||||
templ UploadPage(errMsg string) {
|
||||
@Layout("Buch hochladen", "/") {
|
||||
<section class="page-head">
|
||||
<h2>Buch hochladen</h2>
|
||||
</section>
|
||||
if errMsg != "" {
|
||||
<p class="error">{ errMsg }</p>
|
||||
}
|
||||
<form method="post" action="/upload" enctype="multipart/form-data" class="upload-form">
|
||||
<label for="book">EPUB oder PDF auswählen</label>
|
||||
<input id="book" type="file" name="book" accept=".epub,.pdf" required />
|
||||
<button type="submit" class="btn">Hochladen</button>
|
||||
</form>
|
||||
}
|
||||
}
|
||||
|
||||
templ BookCover(book library.Book, detail bool) {
|
||||
if book.HasCover {
|
||||
<img class={ coverClass(detail) } src={ "/cover/" + book.ID } alt={ "Cover von " + book.Title } loading="lazy" />
|
||||
@@ -40,11 +94,14 @@ templ BookCover(book library.Book, detail bool) {
|
||||
}
|
||||
}
|
||||
|
||||
templ IndexPage(books []library.Book) {
|
||||
templ IndexPage(books []library.Book, canUpload bool) {
|
||||
@Layout("Bibliothek", "") {
|
||||
<section class="page-head">
|
||||
<h2>Meine Bücher</h2>
|
||||
<p class="page-subtitle">Titel, Autor und Cover auf einen Blick.</p>
|
||||
if canUpload {
|
||||
<a class="btn" href="/upload">Buch hochladen</a>
|
||||
}
|
||||
</section>
|
||||
if len(books) == 0 {
|
||||
<p>Keine eBooks gefunden. Lege EPUB- oder PDF-Dateien im Ordner <code>books/</code> ab.</p>
|
||||
|
||||
Reference in New Issue
Block a user