Simplify fullstack auth example (#3598)

This commit is contained in:
Evan Almloff authored and GitHub committed 2025-01-20 16:35:19 -08:00
1 parent eaf9f80abd
commit aba2e73025
3 files changed
+18 -85

No files matched your search

+9 -65
View File
@@ -192,71 +192,15 @@ pub async fn connect_to_database() -> SqlitePool {
.unwrap() .unwrap()
} }
pub struct Session( pub type Session = axum_session_auth::AuthSession<
pub axum_session_auth::AuthSession< crate::auth::User,
crate::auth::User, i64,
i64, axum_session_auth::SessionSqlitePool,
axum_session_auth::SessionSqlitePool, sqlx::SqlitePool,
sqlx::SqlitePool, >;
>,
);
impl std::ops::Deref for Session { pub async fn get_session() -> Result<Session, ServerFnError> {
type Target = axum_session_auth::AuthSession< extract::<Session, _>()
crate::auth::User,
i64,
axum_session_auth::SessionSqlitePool,
sqlx::SqlitePool,
>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for Session {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Debug)]
pub struct AuthSessionLayerNotFound;
impl std::fmt::Display for AuthSessionLayerNotFound {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "AuthSessionLayer was not found")
}
}
impl std::error::Error for AuthSessionLayerNotFound {}
impl IntoResponse for AuthSessionLayerNotFound {
fn into_response(self) -> Response {
(
http::status::StatusCode::INTERNAL_SERVER_ERROR,
"AuthSessionLayer was not found",
)
.into_response()
}
}
#[async_trait]
impl<S: std::marker::Sync + std::marker::Send> axum::extract::FromRequestParts<S> for Session {
type Rejection = AuthSessionLayerNotFound;
async fn from_request_parts(
parts: &mut http::request::Parts,
state: &S,
) -> Result<Self, Self::Rejection> {
axum_session_auth::AuthSession::<
crate::auth::User,
i64,
axum_session_auth::SessionSqlitePool,
sqlx::SqlitePool,
>::from_request_parts(parts, state)
.await .await
.map(Session) .map_err(|_| ServerFnError::new("AuthSessionLayer was not found"))
.map_err(|_| AuthSessionLayerNotFound)
}
} }
+7 -14
View File
@@ -1,10 +1,3 @@
//! Run with:
//!
//! ```sh
//! dx build --features web
//! cargo run --features server
//! ```
#![allow(non_snake_case, unused)] #![allow(non_snake_case, unused)]
#[cfg(feature = "server")] #[cfg(feature = "server")]
@@ -113,23 +106,23 @@ fn app() -> Element {
} }
} }
#[server(GetUserName)] #[server]
pub async fn get_user_name() -> Result<String, ServerFnError> { pub async fn get_user_name() -> Result<String, ServerFnError> {
let session: crate::auth::Session = extract().await?; let auth = auth::get_session().await?;
Ok(session.0.current_user.unwrap().username.to_string()) Ok(auth.current_user.unwrap().username.to_string())
} }
#[server(Login)] #[server]
pub async fn login() -> Result<(), ServerFnError> { pub async fn login() -> Result<(), ServerFnError> {
let auth: crate::auth::Session = extract().await?; let auth = auth::get_session().await?;
auth.login_user(2); auth.login_user(2);
Ok(()) Ok(())
} }
#[server(Permissions)] #[server]
pub async fn get_permissions() -> Result<String, ServerFnError> { pub async fn get_permissions() -> Result<String, ServerFnError> {
let method: axum::http::Method = extract().await?; let method: axum::http::Method = extract().await?;
let auth: crate::auth::Session = extract().await?; let auth = auth::get_session().await?;
let current_user = auth.current_user.clone().unwrap_or_default(); let current_user = auth.current_user.clone().unwrap_or_default();
// lets check permissions only and not worry about if they are anon or not // lets check permissions only and not worry about if they are anon or not
+2 -6
View File
@@ -405,12 +405,8 @@ pub struct Axum;
#[cfg(feature = "axum")] #[cfg(feature = "axum")]
#[async_trait::async_trait] #[async_trait::async_trait]
impl< impl<I: axum::extract::FromRequestParts<()>> FromServerContext<Axum> for I {
I: axum::extract::FromRequestParts<(), Rejection = R>, type Rejection = I::Rejection;
R: axum::response::IntoResponse + std::error::Error,
> FromServerContext<Axum> for I
{
type Rejection = R;
#[allow(clippy::all)] #[allow(clippy::all)]
async fn from_request(req: &DioxusServerContext) -> Result<Self, Self::Rejection> { async fn from_request(req: &DioxusServerContext) -> Result<Self, Self::Rejection> {