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, ()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue