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 | |
parent | 159b239aa2964f6c07438638b967746354376b2e (diff) | |
download | pinussy-5dc4774ed3380762b4d7aadc86193af6073c456a.tar.gz pinussy-5dc4774ed3380762b4d7aadc86193af6073c456a.tar.bz2 pinussy-5dc4774ed3380762b4d7aadc86193af6073c456a.zip |
implement log-out
Diffstat (limited to '')
-rw-r--r-- | src/main.rs | 22 | ||||
-rw-r--r-- | templates/base.rs.html | 8 | ||||
-rw-r--r-- | templates/home.rs.html | 10 | ||||
-rw-r--r-- | templates/login.rs.html | 2 | ||||
-rw-r--r-- | templates/signup.rs.html | 2 | ||||
-rw-r--r-- | templates/users.rs.html | 4 |
6 files changed, 37 insertions, 11 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 { diff --git a/templates/base.rs.html b/templates/base.rs.html index 64a8d4e..0bf70eb 100644 --- a/templates/base.rs.html +++ b/templates/base.rs.html @@ -1,7 +1,7 @@ @use super::statics::*; @use crate::Notification; -@(authenticated: bool, notification: Option<Notification>, body: Content) +@(user: Option<String>, notification: Option<Notification>, body: Content) <!DOCTYPE html> <html> @@ -15,9 +15,9 @@ <body> <nav> - @if authenticated { + @if let Some(username) = user { <form action="/logout" method="post"> - <button type="submit">logout</button> + <button type="submit">logout @username</button> </form> } else { <a href="/login">log in</a> @@ -29,4 +29,4 @@ @:body() </body> -</html>
\ No newline at end of file +</html> diff --git a/templates/home.rs.html b/templates/home.rs.html new file mode 100644 index 0000000..882b2ed --- /dev/null +++ b/templates/home.rs.html @@ -0,0 +1,10 @@ +@use super::base_html; +@use crate::User; + +@(user: Option<String>) + +@:base_html(user.clone(), None, { + @if let Some(username) = user { + <h1>logged in as @username</h1> + } +}) diff --git a/templates/login.rs.html b/templates/login.rs.html index c87866c..8547bd0 100644 --- a/templates/login.rs.html +++ b/templates/login.rs.html @@ -3,7 +3,7 @@ @(notification: Option<Notification>) -@:base_html(false, None, { +@:base_html(None, None, { <form action="/login" method="post"> <label for="username">username:</label> <input type="text" id="username" name="username" required="true" /> diff --git a/templates/signup.rs.html b/templates/signup.rs.html index a482fc2..4823698 100644 --- a/templates/signup.rs.html +++ b/templates/signup.rs.html @@ -3,7 +3,7 @@ @(notification: Option<Notification>) -@:base_html(false, notification, { +@:base_html(None, notification, { <form action="/signup" method="post"> <label for="username">username:</label> <input type="text" id="username" name="username" required="true" /> diff --git a/templates/users.rs.html b/templates/users.rs.html index ab1ab0f..c5b98f9 100644 --- a/templates/users.rs.html +++ b/templates/users.rs.html @@ -3,10 +3,10 @@ @(users: Vec<User>) -@:base_html(false, None, { +@:base_html(None, None, { <ul> @for user in users { <li>@user.username</li> } </ul> -})
\ No newline at end of file +}) |