diff options
Diffstat (limited to 'src')
-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 { |