Was easier to get a db handle than expected

This commit is contained in:
Alex Wright 2020-03-02 00:57:14 +01:00
parent 9a6bd240ad
commit 7b7775e438
2 changed files with 16 additions and 8 deletions

View File

@ -226,14 +226,16 @@ fn oidc_config() -> Json<OidcConfig> {
impl<'a, 'r> FromRequest<'a, 'r> for User {
type Error = ();
fn from_request(_request: &'a Request<'r>) -> Outcome<User, Self::Error> {
Outcome::Success(User {
id: 1,
username: "Alex".to_string(),
is_active: true,
created_at: None,
updated_at: None,
})
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
let mut user_id = match request.cookies().get_private("user_id") {
Some(cookie) => cookie.value().to_string(),
None => return Outcome::Forward(()),
};
let conn = request.guard::<AuthDb>().unwrap();
match User::get_with_id(&conn, user_id) {
Ok(user) => Outcome::Success(user),
_ => Outcome::Forward(()),
}
}
}

View File

@ -25,6 +25,12 @@ pub struct NewUser {
}
impl User {
pub fn get_with_id(conn: &PgConnection, user_id_str: String) -> QueryResult<User> {
use crate::schema::users::dsl::{ id, users };
let user_id = user_id_str.parse::<i32>().unwrap();
users.filter(id.eq(&user_id)).first::<User>(conn)
}
pub fn get_with_username(conn: &PgConnection, name: String) -> QueryResult<User> {
use crate::schema::users::dsl::{ username, users };
users.filter(username.eq(&name)).first::<User>(conn)