From 5e28192a64c753d9f860d4e22b5e546b6549af56 Mon Sep 17 00:00:00 2001 From: Nettika Date: Sun, 25 Jan 2026 17:09:52 -0800 Subject: [PATCH] Add a login button --- TODO.md | 4 ++-- src/main.rs | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 0e2c85b..42661c5 100644 --- a/TODO.md +++ b/TODO.md @@ -15,7 +15,7 @@ [x] Add a rouille::session manager to set a session cookie. [x] Create a `POST /login` endpoint that validates credentials against USERNAME and PASSWORD environment variables. [x] Protect write endpoints (POST /projects, and any future write operations) with authentication. Redirect to /login if not authenticated. -[ ] Add a login button to the front page (GET /projects) that links to /login. -[ ] When logged in, show a "Create Project" button on the front page that links to /new-project. +[x] Add a login button to the front page (GET /projects) that links to /login. +[x] When logged in, show a "Create Project" button on the front page that links to /new-project. [ ] When logged in, add an edit icon to each project-card web component. Clicking the icon opens a dialog with a form to update the progress percentage and archive/unarchive the project. [ ] Create POST endpoints for updating project progress and archiving/unarchiving projects (these will be called from the edit dialog). \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1c4c59a..095c875 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ fn main() { let js = include_bytes!("project-card.js"); rouille::Response::from_data("application/javascript", js.as_ref()) }, - (GET) ["/projects"] => display_projects(), + (GET) ["/projects"] => display_projects(session, &authenticated_sessions), (GET) ["/new-project"] => { if is_authenticated(session, &authenticated_sessions) { new_project_form() @@ -57,15 +57,22 @@ fn main() { }); } -fn is_authenticated(session: &session::Session, authenticated_sessions: &Mutex>) -> bool { +fn is_authenticated( + session: &session::Session, + authenticated_sessions: &Mutex>, +) -> bool { authenticated_sessions .lock() .map(|sessions| sessions.contains(session.id())) .unwrap_or(false) } -fn display_projects() -> rouille::Response { +fn display_projects( + session: &session::Session, + authenticated_sessions: &Mutex>, +) -> rouille::Response { let projects = db::list_all_projects().unwrap_or_default(); + let is_logged_in = is_authenticated(session, authenticated_sessions); let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -93,6 +100,13 @@ fn display_projects() -> rouille::Response { } body { main { + nav { + @if is_logged_in { + a href="/new-project" { strong { "Create Project" } } + } @else { + a href="/login" { strong { "Login" } } + } + } section { @for project in &active_projects { project-card