v1.4 - added admin page and auth
This commit is contained in:
parent
5efdafbb97
commit
1f0aa3078f
29 changed files with 5264 additions and 217 deletions
99
server/src/routes/auth.js
Normal file
99
server/src/routes/auth.js
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
AUTH ROUTES
|
||||
|
||||
POST /api/auth/login email + password → session cookie
|
||||
POST /api/auth/logout deletes the session row and cookie
|
||||
GET /api/auth/me who am I, or 401
|
||||
|
||||
Three deliberate choices:
|
||||
|
||||
Login is rate limited where it's mounted, and answers wrong
|
||||
password and unknown account identically. Telling an attacker
|
||||
which addresses exist is free reconnaissance.
|
||||
|
||||
A failed login still runs a hash. Otherwise the response time
|
||||
itself says whether the account exists.
|
||||
|
||||
Nothing here creates accounts. See admin-cli.js.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { Hono } from "hono";
|
||||
|
||||
import {
|
||||
createSession,
|
||||
destroySession,
|
||||
currentUser,
|
||||
hashPassword,
|
||||
verifyPassword,
|
||||
} from "../auth.js";
|
||||
|
||||
const auth = new Hono();
|
||||
|
||||
// A real hash to compare against when the account doesn't exist,
|
||||
// so both paths cost the same. Computed once at import.
|
||||
const DUMMY_HASH = hashPassword("this password is never correct");
|
||||
|
||||
const NO_STORE = { "Cache-Control": "no-store" };
|
||||
|
||||
auth.post("/login", async (c) => {
|
||||
let body;
|
||||
try {
|
||||
body = await c.req.json();
|
||||
} catch {
|
||||
return c.json({ error: "Expected a JSON body." }, 400);
|
||||
}
|
||||
|
||||
const email = String(body.email ?? "").trim().toLowerCase().slice(0, 254);
|
||||
const password = String(body.password ?? "");
|
||||
|
||||
if (!email || !password) {
|
||||
return c.json({ error: "Email and password are required." }, 400);
|
||||
}
|
||||
|
||||
const db = c.get("db");
|
||||
|
||||
const user = db
|
||||
.prepare(
|
||||
`SELECT id, email, name, role, password_hash, is_active
|
||||
FROM admin_users
|
||||
WHERE email = ?`,
|
||||
)
|
||||
.get(email);
|
||||
|
||||
const ok =
|
||||
user && user.is_active === 1
|
||||
? verifyPassword(password, user.password_hash)
|
||||
: (verifyPassword(password, DUMMY_HASH), false);
|
||||
|
||||
if (!ok) {
|
||||
console.warn(`login failed for ${email}`);
|
||||
return c.json({ error: "That email and password don't match." }, 401);
|
||||
}
|
||||
|
||||
createSession(db, c, user.id);
|
||||
|
||||
db.prepare("UPDATE admin_users SET last_login_at = datetime('now') WHERE id = ?").run(
|
||||
user.id,
|
||||
);
|
||||
|
||||
console.log(`login ${user.email}`);
|
||||
|
||||
return c.json(
|
||||
{ user: { id: user.id, email: user.email, name: user.name, role: user.role } },
|
||||
200,
|
||||
NO_STORE,
|
||||
);
|
||||
});
|
||||
|
||||
auth.post("/logout", (c) => {
|
||||
destroySession(c.get("db"), c);
|
||||
return c.body(null, 204);
|
||||
});
|
||||
|
||||
auth.get("/me", (c) => {
|
||||
const user = currentUser(c.get("db"), c);
|
||||
if (!user) return c.json({ error: "Not signed in." }, 401, NO_STORE);
|
||||
return c.json({ user }, 200, NO_STORE);
|
||||
});
|
||||
|
||||
export default auth;
|
||||
Loading…
Add table
Add a link
Reference in a new issue