parent
439467f730
commit
cd140f0160
4 changed files with 15 additions and 15 deletions
|
@ -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