feat: Redirect to requested URL after login
This commit is contained in:
parent
c28b25d8d9
commit
63e55f814f
8 changed files with 144 additions and 17 deletions
|
|
@ -2,12 +2,13 @@ use axum::extract::{FromRequestParts, OptionalFromRequestParts};
|
|||
use axum::response::Redirect;
|
||||
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
||||
use http::request::Parts;
|
||||
use http::uri::Uri;
|
||||
use uuid::Uuid;
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use crate::db::User;
|
||||
use crate::http::HttpState;
|
||||
use crate::http::{HttpState, InternalRedirect};
|
||||
|
||||
pub const COOKIE_NAME: &str = "lldap_session";
|
||||
|
||||
|
|
@ -91,10 +92,22 @@ impl FromRequestParts<HttpState> for HttpSession {
|
|||
state: &HttpState,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
let cookies = CookieJar::from_request_parts(parts, state).await.unwrap();
|
||||
state
|
||||
.sessions
|
||||
.get_session(&cookies)
|
||||
.ok_or(Redirect::to("/login"))
|
||||
if let Some(session) = state.sessions.get_session(&cookies) {
|
||||
return Ok(session);
|
||||
}
|
||||
|
||||
// Extract the requested URL, turn it into base64, to let the login page
|
||||
// know where to redirect us.
|
||||
// But first, remove the scheme/host/port from URL.
|
||||
let s = Uri::builder()
|
||||
.path_and_query(parts.uri.path_and_query().unwrap().clone())
|
||||
.build()
|
||||
.unwrap();
|
||||
let r = InternalRedirect::from_string(s.to_string());
|
||||
Err(Redirect::to(&format!(
|
||||
"/login?redirect={}",
|
||||
r.to_base64url()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue