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;
#[macro_use]
extern crate rocket;
use rocket::{tokio::time::{sleep, Duration}, fs::FileServer};
use rocket::serde::{Deserialize, json::Json};
use rocket::serde::{json::Json, Deserialize};
use rocket::{
fs::FileServer,
http::CookieJar,
tokio::time::{sleep, Duration},
};
#[get("/")]
fn hello() -> String {
@ -31,21 +35,33 @@ struct LoginInfo {
}
#[post("/login", data = "<info>")]
async fn login(info: Json<LoginInfo>) -> &'static str {
async fn login(info: Json<LoginInfo>, cookies: &CookieJar<'_>) -> &'static str {
let token = cookies.get_private("token");
match token {
Some(t) => {
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 :("
}
}
}
}
#[rocket::main]
async fn main() {
let _rocket = rocket::build()
.mount("/", routes![hello, get_book, delay, login])
.mount("/root", FileServer::from("/home/sadlynotsappho/Projects/Rust/Projects/fossil/web"))
.mount("/root", FileServer::from("./web"))
.launch()
.await
.unwrap();
}

View File

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

View File

@ -5,14 +5,12 @@
<title>login test</title>
<meta charset="UTF-8">
<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>
<body>
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">
<label for="name">Enter your username: </label>
<input type="text" name="username" id="name" required />
@ -27,12 +25,12 @@
</body>
<script defer>
console.log("FUCK YEAH JAVASCRIPT TIME BABYYYYYYY");
document.getElementById("balls").addEventListener("submit", async (event) => {
document.getElementById("login-form").addEventListener("submit", async (event) => {
event.preventDefault();
const username = event.target.username.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",
header: {"Content-Type": "application/json"},
body: JSON.stringify({username, password})