Adding cookie to track session.

Now I can access postgres from the guards I think I can do better.
This commit is contained in:
Alex Wright 2020-03-02 01:14:41 +01:00
parent 7b7775e438
commit c11d8f2f7c
1 changed files with 7 additions and 2 deletions

View File

@ -28,6 +28,10 @@ use num::BigUint;
use openssl::rsa::Rsa; use openssl::rsa::Rsa;
use ldap3::{ LdapConn, Scope, SearchEntry }; use ldap3::{ LdapConn, Scope, SearchEntry };
use rocket::http::{
Cookie,
Cookies,
};
use rocket::request::{ use rocket::request::{
FlashMessage, FlashMessage,
Form, Form,
@ -149,7 +153,7 @@ fn login_form(flash: Option<FlashMessage<'_, '_>>) -> Template {
} }
#[post("/login", data = "<form_data>")] #[post("/login", data = "<form_data>")]
fn login(form_data: Form<LoginData>, conn: AuthDb) -> Result<Redirect, Flash<Redirect>> { fn login(form_data: Form<LoginData>, conn: AuthDb, mut cookies: Cookies) -> Result<Redirect, Flash<Redirect>> {
let auth = BasicAuthentication { let auth = BasicAuthentication {
username: form_data.username.to_owned(), username: form_data.username.to_owned(),
password: form_data.password.to_owned(), password: form_data.password.to_owned(),
@ -165,6 +169,7 @@ fn login(form_data: Form<LoginData>, conn: AuthDb) -> Result<Redirect, Flash<Red
if ! user.is_active { if ! user.is_active {
return Err(Flash::error(Redirect::to(uri!(login_form)), "Account is suspended")); return Err(Flash::error(Redirect::to(uri!(login_form)), "Account is suspended"));
} }
cookies.add_private(Cookie::new("user_id", user.id.to_string()));
println!("User: {:?}", user); println!("User: {:?}", user);
Ok(Redirect::to("/")) Ok(Redirect::to("/"))
@ -227,7 +232,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
type Error = (); type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> { fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
let mut user_id = match request.cookies().get_private("user_id") { let user_id = match request.cookies().get_private("user_id") {
Some(cookie) => cookie.value().to_string(), Some(cookie) => cookie.value().to_string(),
None => return Outcome::Forward(()), None => return Outcome::Forward(()),
}; };