diff options
author | cel 🌸 <cel@blos.sm> | 2023-12-11 13:57:51 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-12-11 13:57:51 +0000 |
commit | 5dc4774ed3380762b4d7aadc86193af6073c456a (patch) | |
tree | cc8af98d94a0566ec19e4ca2f3cfd3800a7c5423 /src/main.rs | |
parent | 159b239aa2964f6c07438638b967746354376b2e (diff) | |
download | pinussy-5dc4774ed3380762b4d7aadc86193af6073c456a.tar.gz pinussy-5dc4774ed3380762b4d7aadc86193af6073c456a.tar.bz2 pinussy-5dc4774ed3380762b4d7aadc86193af6073c456a.zip |
implement log-out
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index 2736c5a..b10d0cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,7 @@ async fn main() -> std::io::Result<()> { // .service(home_auth) .service(get_login) .service(post_login) + .service(post_logout) .service(get_signup) .service(post_signup) .service(get_users) @@ -74,12 +75,19 @@ async fn main() -> std::io::Result<()> { } #[get("/")] -async fn home(session: Session) -> Result<HttpResponse> { +async fn home(session: Session, state: web::Data<Pinussy>) -> Result<HttpResponse> { + let username: Option<String>; if let Some(user_id) = session.get::<i32>("user_id")? { - return Ok(HttpResponse::Ok().body(format!("you are logged in as {}", user_id))); + username = Some( + sqlx::query!("select username from users where id = $1", user_id) + .fetch_one(&state.db) + .await? + .username, + ) } else { - return Ok(HttpResponse::Ok().body("Hello world!")); + username = None } + return Ok(HttpResponse::Ok().body(render!(templates::home_html, username).unwrap())); } #[get("/signup")] @@ -228,6 +236,14 @@ async fn post_login( } } +#[post("/logout")] +async fn post_logout(session: Session) -> HttpResponse { + session.purge(); + HttpResponse::SeeOther() + .insert_header((LOCATION, "/")) + .finish() +} + #[derive(sqlx::Type)] #[sqlx(type_name = "privacy", rename_all = "lowercase")] enum Privacy { |