cargo fmt, fix lint
This commit is contained in:
parent
e83fa37d58
commit
b38c8c663c
@ -4,10 +4,10 @@ extern crate rocket;
|
||||
mod user;
|
||||
|
||||
use rocket::fs::FileServer;
|
||||
use rocket::response::Redirect;
|
||||
use rocket::serde::{json::Json, Serialize};
|
||||
use rocket_db_pools::{sqlx, Database};
|
||||
use rocket_dyn_templates::{context, Template};
|
||||
use rocket::response::Redirect;
|
||||
use user::AuthenticatedUser;
|
||||
|
||||
#[derive(Database)]
|
||||
|
19
src/user.rs
19
src/user.rs
@ -1,9 +1,9 @@
|
||||
use bcrypt;
|
||||
use chrono;
|
||||
use rocket::http::{Cookie, CookieJar, Status};
|
||||
use rocket::serde::{json::Json, Deserialize, Serialize};
|
||||
use rocket_db_pools::Connection;
|
||||
use uuid::Uuid;
|
||||
use bcrypt;
|
||||
|
||||
use crate::Db;
|
||||
|
||||
@ -19,7 +19,12 @@ pub struct User {
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn new(username: String, password_hash: String, email: Option<String>, display_name: Option<String>) -> Self {
|
||||
pub fn new(
|
||||
username: String,
|
||||
password_hash: String,
|
||||
email: Option<String>,
|
||||
display_name: Option<String>,
|
||||
) -> Self {
|
||||
User {
|
||||
id: Uuid::new_v4(),
|
||||
username,
|
||||
@ -69,7 +74,7 @@ pub async fn create_user(
|
||||
new_user.username,
|
||||
password_hash,
|
||||
new_user.email,
|
||||
new_user.display_name
|
||||
new_user.display_name,
|
||||
);
|
||||
|
||||
let query = sqlx::query(
|
||||
@ -214,7 +219,7 @@ pub async fn login(
|
||||
|
||||
#[post("/logout")]
|
||||
pub fn logout(cookies: &CookieJar<'_>) -> Status {
|
||||
cookies.remove_private(Cookie::named("user_id"));
|
||||
cookies.remove_private(Cookie::build("user_id"));
|
||||
Status::NoContent
|
||||
}
|
||||
|
||||
@ -227,7 +232,9 @@ pub struct AuthenticatedUser {
|
||||
impl<'r> rocket::request::FromRequest<'r> for AuthenticatedUser {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'r rocket::Request<'_>) -> rocket::request::Outcome<Self, Self::Error> {
|
||||
async fn from_request(
|
||||
request: &'r rocket::Request<'_>,
|
||||
) -> rocket::request::Outcome<Self, Self::Error> {
|
||||
match request.cookies().get_private("user_id") {
|
||||
Some(cookie) => {
|
||||
if let Ok(user_id) = Uuid::parse_str(cookie.value()) {
|
||||
@ -235,7 +242,7 @@ impl<'r> rocket::request::FromRequest<'r> for AuthenticatedUser {
|
||||
} else {
|
||||
rocket::request::Outcome::Forward(Status::Unauthorized)
|
||||
}
|
||||
},
|
||||
}
|
||||
None => rocket::request::Outcome::Forward(Status::Unauthorized),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user