Add onboarding and FromRequest for User.
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
pub mod channel;
|
||||
pub mod message;
|
||||
pub mod guild;
|
||||
pub mod user;
|
||||
mod channel;
|
||||
mod message;
|
||||
mod guild;
|
||||
mod user;
|
||||
|
||||
pub use channel::*;
|
||||
pub use message::*;
|
||||
pub use guild::*;
|
||||
pub use user::*;
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
use mongodb::bson::{doc, from_bson, Bson};
|
||||
use rauth::auth::Session;
|
||||
use rocket::http::Status;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::database::get_collection;
|
||||
use rocket::request::{self, FromRequest, Outcome, Request};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Relationship {
|
||||
@@ -10,6 +15,63 @@ pub struct Relationship {
|
||||
pub struct User {
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
pub username: Option<String>,
|
||||
pub username: String,
|
||||
pub relations: Option<Vec<Relationship>>,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for User {
|
||||
type Error = rauth::util::Error;
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let session: Session = try_outcome!(request.guard::<Session>().await);
|
||||
|
||||
if let Ok(result) = get_collection("users")
|
||||
.find_one(
|
||||
doc! {
|
||||
"_id": &session.user_id
|
||||
}, None
|
||||
)
|
||||
.await {
|
||||
if let Some(doc) = result {
|
||||
Outcome::Success(
|
||||
from_bson(Bson::Document(doc)).unwrap()
|
||||
)
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
||||
}
|
||||
} else {
|
||||
Outcome::Failure((Status::InternalServerError, rauth::util::Error::DatabaseError))
|
||||
}
|
||||
|
||||
/*Outcome::Success(
|
||||
User {
|
||||
id: "gaming".to_string(),
|
||||
username: None,
|
||||
relations: None
|
||||
}
|
||||
)*/
|
||||
|
||||
/*match (
|
||||
request.managed_state::<Auth>(),
|
||||
header_user_id,
|
||||
header_session_token,
|
||||
) {
|
||||
(Some(auth), Some(user_id), Some(session_token)) => {
|
||||
let session = Session {
|
||||
id: None,
|
||||
user_id,
|
||||
session_token,
|
||||
};
|
||||
|
||||
if let Ok(session) = auth.verify_session(session).await {
|
||||
Outcome::Success(session)
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, Error::InvalidSession))
|
||||
}
|
||||
}
|
||||
(None, _, _) => Outcome::Failure((Status::InternalServerError, Error::InternalError)),
|
||||
(_, _, _) => Outcome::Failure((Status::Forbidden, Error::MissingHeaders)),
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user