Skip to content

Commit

Permalink
Shield Leptos/Tower: Fix session
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Dec 25, 2024
1 parent 1c01111 commit 235e278
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
13 changes: 13 additions & 0 deletions packages/integrations/shield-leptos/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::sync::Arc;

use leptos::prelude::expect_context;
use shield::{ServerIntegration, Shield};

pub fn expect_server_integration() -> Arc<dyn ServerIntegration> {
expect_context::<Arc<dyn ServerIntegration>>()
}

pub async fn expect_shield() -> Shield {
let server_integration = expect_server_integration();
server_integration.extract_shield().await
}
1 change: 1 addition & 0 deletions packages/integrations/shield-leptos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod context;
pub mod routes;
13 changes: 5 additions & 8 deletions packages/integrations/shield-leptos/src/routes/sign_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ use shield::SubproviderVisualisation;

#[server]
pub async fn subproviders() -> Result<Vec<SubproviderVisualisation>, ServerFnError> {
use std::sync::Arc;
use crate::context::expect_shield;

use shield::ServerIntegration;

let server_integration = expect_context::<Arc<dyn ServerIntegration>>();
let shield = server_integration.extract_shield().await;
let shield = expect_shield().await;

shield
.subprovider_visualisations()
Expand All @@ -21,11 +18,11 @@ pub async fn sign_in(
provider_id: String,
subprovider_id: Option<String>,
) -> Result<(), ServerFnError> {
use std::sync::Arc;
use shield::{Response, ShieldError, SignInRequest};

use shield::{Response, ServerIntegration, ShieldError, SignInRequest};
use crate::context::expect_server_integration;

let server_integration = expect_context::<Arc<dyn ServerIntegration>>();
let server_integration = expect_server_integration();
let shield = server_integration.extract_shield().await;
let session = server_integration.extract_session().await;

Expand Down
7 changes: 4 additions & 3 deletions packages/integrations/shield-tower/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use http::{Request, Response};
use shield::Shield;
use shield::{Session, Shield};
use tower_service::Service;

use crate::session::TowerSession;
Expand Down Expand Up @@ -66,10 +66,11 @@ where
}
};

let shield_session = match TowerSession::load(session.clone(), session_key).await {
Ok(shield_session) => shield_session,
let session_storage = match TowerSession::load(session.clone(), session_key).await {
Ok(session_storage) => session_storage,
Err(_err) => return Ok(Self::internal_server_error()),
};
let shield_session = Session::new(session_storage);

req.extensions_mut().insert(shield);
req.extensions_mut().insert(shield_session);
Expand Down

0 comments on commit 235e278

Please sign in to comment.