Was easier to get a db handle than expected
This commit is contained in:
parent
9a6bd240ad
commit
7b7775e438
18
src/main.rs
18
src/main.rs
|
@ -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(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue