add logout button and functionality
This commit is contained in:
parent
c759787867
commit
57f3fe601d
|
@ -1 +1,2 @@
|
|||
/target
|
||||
Rocket.toml
|
||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -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();
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
:root {
|
||||
background-color: red;
|
||||
color: lime;
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
@ -35,8 +37,15 @@
|
|||
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>
|
||||
|
|
Loading…
Reference in New Issue