Added logout feature

This commit is contained in:
Lucas Schumacher 2023-10-30 13:43:52 -04:00
parent 16dac8941b
commit 1dd0b7caaa
2 changed files with 27 additions and 4 deletions

View File

@ -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()
}
</script>
@ -33,6 +47,7 @@ async function click(ev) {
<main class="form-signin w-100 m-auto">
<p class="mt-5 mb-3 text-body">You have accessed this content {{ count }} times this session.</p>
<button type="submit" @click="click" class="btn btn-primary w-100 py-2">Refresh Content</button>
<button type="submit" @click="signout" class="btn btn-primary w-100 py-2">Sign out</button>
</main>
</template>

View File

@ -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<UserLogin>, session: &Session) -> UserResponse {
let password = user.password.as_str();
@ -249,10 +256,11 @@ impl Api {
if let None = session.get::<String>("user") {
return NumResponse::AuthError;
}
match session.get("num") {
match session.get::<u32>("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);