Forráskód Böngészése

Simplify fullstack auth example (#3598)

Evan Almloff 5 hónapja
szülő
commit
aba2e73025

+ 10 - 66
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,
-    >,
-);
-
-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<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)
+pub type Session = axum_session_auth::AuthSession<
+    crate::auth::User,
+    i64,
+    axum_session_auth::SessionSqlitePool,
+    sqlx::SqlitePool,
+>;
+
+pub async fn get_session() -> Result<Session, ServerFnError> {
+    extract::<Session, _>()
         .await
-        .map(Session)
-        .map_err(|_| AuthSessionLayerNotFound)
-    }
+        .map_err(|_| ServerFnError::new("AuthSessionLayer was not found"))
 }

+ 7 - 14
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<String, ServerFnError> {
-    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<String, ServerFnError> {
     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

+ 2 - 6
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<Axum> for I
-{
-    type Rejection = R;
+impl<I: axum::extract::FromRequestParts<()>> FromServerContext<Axum> for I {
+    type Rejection = I::Rejection;
 
     #[allow(clippy::all)]
     async fn from_request(req: &DioxusServerContext) -> Result<Self, Self::Rejection> {