Implement login form

This commit is contained in:
Nettika 2026-01-25 15:43:59 -08:00
parent 4c43806419
commit a778d590f8
No known key found for this signature in database
2 changed files with 29 additions and 1 deletions

View file

@ -11,7 +11,7 @@
[x] Create a `POST /projects` endpoint that accepts URL encoded data and creates a new project.
[x] Create a `GET /new-project` endpoint that returns a HTML page with a project creation form. Keep it simple.
[x] Add .env file support using the dotenv crate. Load environment variables on server startup.
[ ] Create a `GET /login` endpoint that returns a simple HTML login form (username and password fields).
[x] Create a `GET /login` endpoint that returns a simple HTML login form (username and password fields).
[ ] Create a `POST /login` endpoint that validates credentials against USERNAME and PASSWORD environment variables. On success, set a session cookie.
[ ] Implement session management. Create a simple in-memory session store that tracks authenticated sessions by cookie token.
[ ] Add authentication middleware or helper function to check if a request has a valid session cookie.

View file

@ -28,6 +28,7 @@ fn main() {
},
(GET) ["/projects"] => display_projects(),
(GET) ["/new-project"] => new_project_form(),
(GET) ["/login"] => login_form(),
(POST) ["/projects"] => create_project(request),
_ => rouille::Response::empty_404()
)
@ -125,6 +126,33 @@ fn new_project_form() -> rouille::Response {
rouille::Response::html(markup.into_string())
}
fn login_form() -> rouille::Response {
let markup = html! {
(DOCTYPE)
html {
head {
meta charset="utf-8";
meta name="viewport" content="width=device-width, initial-scale=1";
link rel="stylesheet" href="/main.css";
}
body {
main {
h1 { "Login" }
form method="POST" action="/login" {
label for="username" { "Username:" }
input type="text" id="username" name="username" required;
label for="password" { "Password:" }
input type="password" id="password" name="password" required;
button type="submit" { "Login" }
}
}
}
}
};
rouille::Response::html(markup.into_string())
}
fn create_project(request: &rouille::Request) -> rouille::Response {
let input = try_or_400!(rouille::post_input!(request, {
title: String,