parent
439467f730
commit
cd140f0160
4 changed files with 15 additions and 15 deletions
|
@ -33,7 +33,7 @@ pub enum SessionKind {
|
|||
}
|
||||
|
||||
pub trait Sessions {
|
||||
fn from_session(session: &str) -> impl std::future::Future<Output = Option<User>>;
|
||||
fn from_session(session: String) -> impl std::future::Future<Output = Option<User>>;
|
||||
fn login(
|
||||
username: &str,
|
||||
password: &str,
|
||||
|
@ -78,11 +78,11 @@ impl Sessions for User {
|
|||
}
|
||||
|
||||
// Get a user from session ID
|
||||
async fn from_session(session: &str) -> Option<User> {
|
||||
let user: Option<Self> = sqlx::query_as("SELECT * FROM users WHERE username = (SELECT \"user\" FROM user_session WHERE token = $1)").bind(session).fetch_optional(get_pg!()).await.unwrap();
|
||||
async fn from_session(session: String) -> Option<User> {
|
||||
let user: Option<Self> = sqlx::query_as("SELECT * FROM users WHERE username = (SELECT \"user\" FROM user_session WHERE token = $1)").bind(&session).fetch_optional(get_pg!()).await.unwrap();
|
||||
|
||||
if let Some(mut user) = user {
|
||||
user.session = session.to_string();
|
||||
user.session = session;
|
||||
return Some(user);
|
||||
}
|
||||
|
||||
|
|
|
@ -54,14 +54,14 @@ impl User {
|
|||
/// Create a new user with the given details
|
||||
///
|
||||
/// Returns an Option containing the created user, or None if a user already exists with the same username
|
||||
pub async fn create(username: &str, password: &str, role: UserRole) -> Option<Self> {
|
||||
pub async fn create(username: String, password: &str, role: UserRole) -> Option<Self> {
|
||||
// Check if a user already exists with the same username
|
||||
if Self::find(username).await.is_some() {
|
||||
if Self::find(&username).await.is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let u = Self {
|
||||
username: username.to_string(),
|
||||
username,
|
||||
password: bcrypt::hash(password, bcrypt::DEFAULT_COST).unwrap(),
|
||||
user_role: role,
|
||||
session: String::new(),
|
||||
|
@ -133,7 +133,7 @@ impl ToAPI for User {
|
|||
/// extracts a user from a request with `session` cookie
|
||||
async fn extract_user(request: &Request<'_>) -> Option<User> {
|
||||
if let Some(session_id) = request.cookies().get("session") {
|
||||
if let Some(user) = User::from_session(session_id.value()).await {
|
||||
if let Some(user) = User::from_session(session_id.value().to_string()).await {
|
||||
return Some(user);
|
||||
}
|
||||
return None;
|
||||
|
@ -164,7 +164,7 @@ impl<'r> FromRequest<'r> for APIUser {
|
|||
async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome<Self, Self::Error> {
|
||||
match request.headers().get_one("token") {
|
||||
Some(key) => {
|
||||
if let Some(user) = User::from_session(key).await {
|
||||
if let Some(user) = User::from_session(key.to_string()).await {
|
||||
return Outcome::Success(APIUser(user));
|
||||
}
|
||||
return Outcome::Error((Status::Unauthorized, ()));
|
||||
|
|
|
@ -15,10 +15,10 @@ pub struct DataResponse {
|
|||
|
||||
impl DataResponse {
|
||||
#[must_use]
|
||||
pub fn new(data: Vec<u8>, content_type: &str, cache_duration: Option<u64>) -> Self {
|
||||
pub fn new(data: Vec<u8>, content_type: String, cache_duration: Option<u64>) -> Self {
|
||||
Self {
|
||||
data,
|
||||
content_type: content_type.to_string(),
|
||||
content_type,
|
||||
cache_duration,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,8 +67,8 @@ pub fn respond_json(json: &serde_json::Value) -> StringResponse {
|
|||
/// # Returns
|
||||
/// A `StringResponse` with status `200 OK`, content type `text/html`, and the HTML content as the body.
|
||||
#[must_use]
|
||||
pub fn respond_html(html: &str) -> StringResponse {
|
||||
(Status::Ok, (ContentType::HTML, html.to_string()))
|
||||
pub fn respond_html(html: String) -> StringResponse {
|
||||
(Status::Ok, (ContentType::HTML, html))
|
||||
}
|
||||
|
||||
/// Helper function to create an JS HTTP response.
|
||||
|
@ -79,8 +79,8 @@ pub fn respond_html(html: &str) -> StringResponse {
|
|||
/// # Returns
|
||||
/// A `StringResponse` with status `200 OK`, content type `text/javascript`, and the JS content as the body.
|
||||
#[must_use]
|
||||
pub fn respond_script(script: &str) -> StringResponse {
|
||||
(Status::Ok, (ContentType::JavaScript, script.to_string()))
|
||||
pub fn respond_script(script: String) -> StringResponse {
|
||||
(Status::Ok, (ContentType::JavaScript, script))
|
||||
}
|
||||
|
||||
/// Creates a custom HTTP response with the specified status, content type, and body.
|
||||
|
|
Loading…
Add table
Reference in a new issue