diff --git a/examples/fullstack-auth/src/auth.rs b/examples/fullstack-auth/src/auth.rs index 9111ce121..b97168628 100644 --- a/examples/fullstack-auth/src/auth.rs +++ b/examples/fullstack-auth/src/auth.rs @@ -192,71 +192,15 @@ pub async fn connect_to_database() -> SqlitePool { .unwrap() } -pub struct Session( - pub axum_session_auth::AuthSession< - crate::auth::User, - i64, - axum_session_auth::SessionSqlitePool, - sqlx::SqlitePool, - >, -); +pub type Session = axum_session_auth::AuthSession< + crate::auth::User, + i64, + axum_session_auth::SessionSqlitePool, + sqlx::SqlitePool, +>; -impl std::ops::Deref for Session { - type Target = axum_session_auth::AuthSession< - 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 axum::extract::FromRequestParts for Session { - type Rejection = AuthSessionLayerNotFound; - - async fn from_request_parts( - parts: &mut http::request::Parts, - state: &S, - ) -> Result { - axum_session_auth::AuthSession::< - crate::auth::User, - i64, - axum_session_auth::SessionSqlitePool, - sqlx::SqlitePool, - >::from_request_parts(parts, state) +pub async fn get_session() -> Result { + extract::() .await - .map(Session) - .map_err(|_| AuthSessionLayerNotFound) - } + .map_err(|_| ServerFnError::new("AuthSessionLayer was not found")) } diff --git a/examples/fullstack-auth/src/main.rs b/examples/fullstack-auth/src/main.rs index e872003e5..b987d7df8 100644 --- a/examples/fullstack-auth/src/main.rs +++ b/examples/fullstack-auth/src/main.rs @@ -1,10 +1,3 @@ -//! Run with: -//! -//! ```sh -//! dx build --features web -//! cargo run --features server -//! ``` - #![allow(non_snake_case, unused)] #[cfg(feature = "server")] @@ -113,23 +106,23 @@ fn app() -> Element { } } -#[server(GetUserName)] +#[server] pub async fn get_user_name() -> Result { - let session: crate::auth::Session = extract().await?; - Ok(session.0.current_user.unwrap().username.to_string()) + let auth = auth::get_session().await?; + Ok(auth.current_user.unwrap().username.to_string()) } -#[server(Login)] +#[server] pub async fn login() -> Result<(), ServerFnError> { - let auth: crate::auth::Session = extract().await?; + let auth = auth::get_session().await?; auth.login_user(2); Ok(()) } -#[server(Permissions)] +#[server] pub async fn get_permissions() -> Result { 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(); // lets check permissions only and not worry about if they are anon or not diff --git a/packages/fullstack/src/server_context.rs b/packages/fullstack/src/server_context.rs index 08db028ee..1d4d5c479 100644 --- a/packages/fullstack/src/server_context.rs +++ b/packages/fullstack/src/server_context.rs @@ -405,12 +405,8 @@ pub struct Axum; #[cfg(feature = "axum")] #[async_trait::async_trait] -impl< - I: axum::extract::FromRequestParts<(), Rejection = R>, - R: axum::response::IntoResponse + std::error::Error, - > FromServerContext for I -{ - type Rejection = R; +impl> FromServerContext for I { + type Rejection = I::Rejection; #[allow(clippy::all)] async fn from_request(req: &DioxusServerContext) -> Result {