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 ldap3::{ LdapConn, Scope, SearchEntry };
use rocket::http::{
Cookie,
Cookies,
};
use rocket::request::{
FlashMessage,
Form,
@ -149,7 +153,7 @@ fn login_form(flash: Option<FlashMessage<'_, '_>>) -> Template {
}
#[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 {
username: form_data.username.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 {
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);
Ok(Redirect::to("/"))
@ -227,7 +232,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
type 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(),
None => return Outcome::Forward(()),
};