-
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.
- Loading branch information
1 parent
e9ae2f4
commit 797aac7
Showing
7 changed files
with
77 additions
and
127 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub mod authorization_header; | ||
pub mod session_cookie; | ||
pub mod session_user; |
This file was deleted.
Oops, something went wrong.
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,33 @@ | ||
use axum::{ | ||
async_trait, | ||
extract::FromRequestParts, | ||
http::{self, request::Parts, StatusCode}, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use tower_sessions::Session; | ||
|
||
const USER_ID: &str = "user_id"; | ||
|
||
#[derive(Debug, Default, Deserialize, Serialize)] | ||
pub struct SessionUser { | ||
pub id: String, | ||
} | ||
|
||
#[async_trait] | ||
impl<S> FromRequestParts<S> for SessionUser | ||
where | ||
S: Send + Sync, | ||
{ | ||
type Rejection = (http::StatusCode, &'static str); | ||
|
||
async fn from_request_parts(req: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
let session = Session::from_request_parts(req, state).await?; | ||
let user_id: String = session | ||
.get(USER_ID) | ||
.await | ||
.unwrap() | ||
.ok_or((StatusCode::UNAUTHORIZED, "User not authenticated"))?; | ||
|
||
Ok(SessionUser { id: user_id }) | ||
} | ||
} |
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
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