changeset 1:b587a4c37254

Implement logout, add TOODs
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 08 Jul 2022 20:28:28 -0700
parents 3f4f2ba2da92
children 93ea889e6009
files TODO index.html login.html src/main.rs
diffstat 4 files changed, 37 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TODO	Fri Jul 08 20:28:28 2022 -0700
@@ -0,0 +1,5 @@
+* Set up logging
+* Start database layer
+* Enable checked user login
+* Start templates
+* Figure out counting pixel
--- a/index.html	Fri Jul 08 20:19:14 2022 -0700
+++ b/index.html	Fri Jul 08 20:28:28 2022 -0700
@@ -4,5 +4,11 @@
     </head>
     <body>
         Welcome to anaLyrics.
+
+        <a href="/login">Log in</a>
+
+        <form action="/logout" method="POST">
+            <input type="submit" value="Log out" />
+        </form>
     </body>
 </html>
--- a/login.html	Fri Jul 08 20:19:14 2022 -0700
+++ b/login.html	Fri Jul 08 20:28:28 2022 -0700
@@ -4,5 +4,12 @@
     </head>
     <body>
 
+        <form action="/login" method="POST">
+            <label>User name: </label><input type="text" name="username" />
+            <label>Password: </label><input type="text" name="password" />
+            <input type="submit" value="Log in" />
+        </form>
+
+
     </body>
 </html>
--- a/src/main.rs	Fri Jul 08 20:19:14 2022 -0700
+++ b/src/main.rs	Fri Jul 08 20:28:28 2022 -0700
@@ -1,10 +1,10 @@
 #[macro_use]
 use rocket::{self};
 
-use rocket::form::{Form};
+use rocket::form::Form;
 use rocket::http::{Cookie, CookieJar, Header, Status};
+use rocket::request::{self, FlashMessage};
 use rocket::response::{self, Flash, Redirect, Responder, Response};
-use rocket::request::{self, FlashMessage};
 
 use rocket_db_pools::sqlx;
 use rocket_db_pools::{Connection, Database};
@@ -56,6 +56,14 @@
     }
 }
 
+#[rocket::post("/logout")]
+fn route_logout(cookies: &CookieJar<'_>) -> Redirect {
+    if let Some(cookie) = cookies.get_private(USER_ID_COOKIE_KEY) {
+        cookies.remove_private(cookie);
+    }
+    Redirect::to(rocket::uri!("/"))
+}
+
 #[derive(rocket::FromForm)]
 struct LoginForm {
     username: String,
@@ -80,7 +88,13 @@
 
 #[rocket::launch]
 fn rocketmain() -> _ {
-    rocket::build()
-        .attach(ConfigDB::init())
-        .mount("/", rocket::routes![route_index, route_login_form, route_login_post])
+    rocket::build().attach(ConfigDB::init()).mount(
+        "/",
+        rocket::routes![
+            route_index,
+            route_logout,
+            route_login_form,
+            route_login_post
+        ],
+    )
 }