cookie work

This commit is contained in:
SadlyNotSappho 2023-11-28 15:41:16 -08:00
parent 0ea65fcddf
commit c759787867
3 changed files with 31 additions and 15 deletions

View File

@ -3,8 +3,12 @@
use std::fs; use std::fs;
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
use rocket::{tokio::time::{sleep, Duration}, fs::FileServer}; use rocket::serde::{json::Json, Deserialize};
use rocket::serde::{Deserialize, json::Json}; use rocket::{
fs::FileServer,
http::CookieJar,
tokio::time::{sleep, Duration},
};
#[get("/")] #[get("/")]
fn hello() -> String { fn hello() -> String {
@ -31,11 +35,24 @@ struct LoginInfo {
} }
#[post("/login", data = "<info>")] #[post("/login", data = "<info>")]
async fn login(info: Json<LoginInfo>) -> &'static str { async fn login(info: Json<LoginInfo>, cookies: &CookieJar<'_>) -> &'static str {
if info.username == "sadlynotsappho" && info.password == "thebestpasswordofalltime" { let token = cookies.get_private("token");
"logged in!" match token {
} else { Some(t) => {
"invalid login info :(" if t.value_trimmed() == "skyetoken" {
"logged in with token"
} else {
"unknown token"
}
}
None => {
if info.username == "sadlynotsappho" && info.password == "thebestpasswordofalltime" {
cookies.add_private(("token", "skyetoken"));
"logged in!"
} else {
"invalid login info :("
}
}
} }
} }
@ -43,9 +60,8 @@ async fn login(info: Json<LoginInfo>) -> &'static str {
async fn main() { async fn main() {
let _rocket = rocket::build() let _rocket = rocket::build()
.mount("/", routes![hello, get_book, delay, login]) .mount("/", routes![hello, get_book, delay, login])
.mount("/root", FileServer::from("/home/sadlynotsappho/Projects/Rust/Projects/fossil/web")) .mount("/root", FileServer::from("./web"))
.launch() .launch()
.await .await
.unwrap(); .unwrap();
} }

View File

@ -0,0 +1,2 @@
:root {
}

View File

@ -5,14 +5,12 @@
<title>login test</title> <title>login test</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/style.css" rel="stylesheet"> <link href="/root/css/style.css" rel="stylesheet">
</head> </head>
<body> <body>
hiii hiii
<form action="http://127.0.0.1:8000/login" method="post" class="form-test" id="balls"> <form action="/login" method="post" class="login-form" id="login-form">
<div class="form-test"> <div class="form-test">
<label for="name">Enter your username: </label> <label for="name">Enter your username: </label>
<input type="text" name="username" id="name" required /> <input type="text" name="username" id="name" required />
@ -27,12 +25,12 @@
</body> </body>
<script defer> <script defer>
console.log("FUCK YEAH JAVASCRIPT TIME BABYYYYYYY"); console.log("FUCK YEAH JAVASCRIPT TIME BABYYYYYYY");
document.getElementById("balls").addEventListener("submit", async (event) => { document.getElementById("login-form").addEventListener("submit", async (event) => {
event.preventDefault(); event.preventDefault();
const username = event.target.username.value; const username = event.target.username.value;
const password = event.target.password.value; const password = event.target.password.value;
const token = await fetch("http://127.0.0.1:8000/login", { const token = await fetch("/login", {
method: "POST", method: "POST",
header: {"Content-Type": "application/json"}, header: {"Content-Type": "application/json"},
body: JSON.stringify({username, password}) body: JSON.stringify({username, password})