add logout button and functionality

This commit is contained in:
SadlyNotSappho 2023-12-05 11:18:14 -08:00
parent c759787867
commit 57f3fe601d
4 changed files with 29 additions and 7 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
Rocket.toml

View File

@ -55,12 +55,22 @@ async fn login(info: Json<LoginInfo>, cookies: &CookieJar<'_>) -> &'static str {
}
}
}
#[post("/logout")]
async fn logout(cookies: &CookieJar<'_>) -> &'static str {
let token = cookies.get_private("token");
if token.is_some() {
cookies.remove_private("token");
"logged out"
} else {
"not logged in"
}
}
#[rocket::main]
async fn main() {
let _rocket = rocket::build()
.mount("/", routes![hello, get_book, delay, login])
.mount("/root", FileServer::from("./web"))
.mount("/", routes![hello, get_book, delay, login, logout])
.mount("/login", FileServer::from("./web"))
.launch()
.await
.unwrap();

View File

@ -1,2 +1,4 @@
:root {
background-color: red;
color: lime;
}

View File

@ -5,7 +5,7 @@
<title>login test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/root/css/style.css" rel="stylesheet">
<link href="/login/css/style.css" rel="stylesheet">
</head>
<body>
@ -22,6 +22,8 @@
<div class="form-test">
<input type="submit" value="Log in" />
</div>
</form>
<button id="logout-button">Log Out</button>
</body>
<script defer>
console.log("FUCK YEAH JAVASCRIPT TIME BABYYYYYYY");
@ -31,12 +33,19 @@
const password = event.target.password.value;
const token = await fetch("/login", {
method: "POST",
header: {"Content-Type": "application/json"},
method: "POST",
header: {"Content-Type": "application/json"},
body: JSON.stringify({username, password})
})
console.log(await token.text())
})
console.log(await token.text());
});
document.getElementById("logout-button").addEventListener("click", async (event) => {
event.preventDefault();
const loggedout = await fetch("/logout", {
method: "POST"
});
console.log(await loggedout.text());
});
</script>
</html>