-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor authentication stuff into own module.
- Loading branch information
Showing
3 changed files
with
46 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use axum::{ | ||
http::{Request, StatusCode}, | ||
middleware::Next, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use typed_session_axum::ReadableSession; | ||
|
||
use super::{session::RVocSessionData, user::model::Username}; | ||
|
||
pub async fn ensure_logged_in<B>(mut request: Request<B>, next: Next<B>) -> Response { | ||
let session: &ReadableSession<RVocSessionData> = request.extensions().get().unwrap(); | ||
|
||
match session.data() { | ||
RVocSessionData::Anonymous => return StatusCode::UNAUTHORIZED.into_response(), | ||
RVocSessionData::LoggedIn(username) => { | ||
let username = username.clone(); | ||
request.extensions_mut().insert(LoggedInUser(username)); | ||
} | ||
} | ||
|
||
next.run(request).await | ||
} | ||
|
||
/// If this extension is found, it means that the request was made by the contained username. | ||
#[derive(Debug, Clone)] | ||
pub struct LoggedInUser(Username); | ||
|
||
impl From<LoggedInUser> for String { | ||
fn from(value: LoggedInUser) -> Self { | ||
value.0.into() | ||
} | ||
} | ||
|
||
impl AsRef<str> for LoggedInUser { | ||
fn as_ref(&self) -> &str { | ||
self.0.as_ref() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters