From 1dd0b7caaafc573123d418e53b9b1469ecaf5efc Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Mon, 30 Oct 2023 13:43:52 -0400 Subject: [PATCH] Added logout feature --- client/src/components/Clicker.vue | 15 +++++++++++++++ src/main.rs | 16 ++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/client/src/components/Clicker.vue b/client/src/components/Clicker.vue index 7c474d1..d107440 100644 --- a/client/src/components/Clicker.vue +++ b/client/src/components/Clicker.vue @@ -26,6 +26,20 @@ async function click(ev) { count.value = await get_clicks() } +async function signout(ev) { + ev.preventDefault() + + const api_url = '/api/auth' + const fetch_options = { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json' + } + } + let resp = await fetch(api_url, fetch_options) + console.log(resp) + window.location.reload() +} @@ -33,6 +47,7 @@ async function click(ev) {

You have accessed this content {{ count }} times this session.

+
diff --git a/src/main.rs b/src/main.rs index 7aab609..469b92f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use futures_util::{SinkExt, StreamExt}; use poem::{ //get, - handler, //Result, + handler, listener::TcpListener, session::{CookieConfig, MemoryStorage, ServerSession, Session}, web::{ @@ -12,6 +12,7 @@ use poem::{ }, EndpointExt, IntoResponse, + Result, Route, Server, }; @@ -222,6 +223,12 @@ impl Api { UserResponse::AuthError } } + #[oai(path = "/auth", method = "delete", tag = "ApiTags::User")] + async fn deauth_user(&self, session: &Session) -> Result<()> { + session.purge(); + Result::Ok(()) + } + #[oai(path = "/auth", method = "post", tag = "ApiTags::User")] async fn auth_user(&self, user: Json, session: &Session) -> UserResponse { let password = user.password.as_str(); @@ -249,10 +256,11 @@ impl Api { if let None = session.get::("user") { return NumResponse::AuthError; } - match session.get("num") { + match session.get::("num") { Some(i) => { - session.set("num", i + 1); - NumResponse::Ok(Json(i)) + let new: u32 = i + 1; + session.set("num", new); + NumResponse::Ok(Json(new)) } None => { session.set("num", 1_u32);