Skip to content

Commit 8a59e76

Browse files
Shield Leptos: Fix hydration
1 parent 878ac5a commit 8a59e76

File tree

17 files changed

+59
-112
lines changed

17 files changed

+59
-112
lines changed

Cargo.lock

Lines changed: 1 addition & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ tower-service = "0.3.3"
3737
tower-sessions = "0.13.0"
3838
tracing = "0.1.41"
3939
tracing-subscriber = "0.3.19"
40-
typetag = "0.2.19"
4140
uuid = "1.11.0"
4241
wasm-bindgen = "0.2.97"
4342
wasm-tracing = "1.0.1"

examples/leptos-actix/src/home.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::sync::Arc;
2-
31
use leptos::{either::Either, prelude::*};
42
use leptos_router::components::A;
3+
use shield_leptos::integration::LeptosUser;
54

65
#[server]
7-
pub async fn user() -> Result<Option<Arc<dyn shield::User>>, ServerFnError> {
6+
pub async fn user() -> Result<Option<LeptosUser>, ServerFnError> {
87
use shield_leptos::context::extract_user;
98

109
Ok(extract_user().await)
@@ -20,7 +19,9 @@ pub fn HomePage() -> impl IntoView {
2019
<Suspense fallback=|| view! { "Loading..." }>
2120
{move || Suspend::new(async move { match user.await {
2221
Ok(user) => Either::Left(match user {
23-
Some(_user) => Either::Left(view! {
22+
Some(user) => Either::Left(view! {
23+
{user.id}
24+
2425
<A href="/auth/sign-out">
2526
<button>"Sign out"</button>
2627
</A>

examples/leptos-axum/src/home.rs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
use std::sync::Arc;
2-
3-
use leptos::prelude::*;
4-
use tracing::debug;
1+
use leptos::{either::Either, prelude::*};
2+
use leptos_router::components::A;
3+
use shield_leptos::integration::LeptosUser;
54

65
#[server]
7-
pub async fn user() -> Result<Option<Arc<dyn shield::User>>, ServerFnError> {
6+
pub async fn user() -> Result<Option<LeptosUser>, ServerFnError> {
87
use shield_leptos::context::extract_user;
98

10-
let user = extract_user().await;
11-
debug!("action {:?}", user.as_ref().map(|user| user.id()));
12-
13-
Ok(user)
9+
Ok(extract_user().await)
1410
}
1511

1612
#[component]
@@ -20,35 +16,26 @@ pub fn HomePage() -> impl IntoView {
2016
view! {
2117
<h1>"Shield Leptos Axum Example"</h1>
2218

23-
{move || match user.get() {
24-
Some(user) => {
25-
debug!("{:?}", user);
26-
match user {
27-
Ok(user) => view! {
28-
{user.as_ref().map(|user| user.id())}
29-
}.into_any(),
30-
Err(err) => view! {
31-
{err.to_string()}
32-
}.into_any(),
33-
}
34-
}.into_any(),
35-
None => view! { "Loading..." }.into_any(),
36-
}}
37-
38-
// <Suspense fallback=move || view! { "Loading..." }>
39-
// {move || Suspend::new(async move {
40-
// let user = user.await;
41-
// debug!("view {:?}", user);
42-
43-
// match user {
44-
// Ok(user) => Either::Left(view! {
45-
// {user.as_ref().map(|user| user.id())}
46-
// }),
47-
// Err(err) => Either::Right(view! {
48-
// {err.to_string()}
49-
// })
50-
// }
51-
// })}
52-
// </Suspense>
19+
<Suspense fallback=|| view! { "Loading..." }>
20+
{move || Suspend::new(async move { match user.await {
21+
Ok(user) => Either::Left(match user {
22+
Some(user) => Either::Left(view! {
23+
{user.id}
24+
25+
<A href="/auth/sign-out">
26+
<button>"Sign out"</button>
27+
</A>
28+
}),
29+
None => Either::Right(view! {
30+
<A href="/auth/sign-in">
31+
<button>"Sign in"</button>
32+
</A>
33+
}),
34+
}),
35+
Err(err) => Either::Right(view! {
36+
{err.to_string()}
37+
})
38+
}})}
39+
</Suspense>
5340
}
5441
}

packages/core/shield/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ serde.workspace = true
1616
serde_json.workspace = true
1717
thiserror.workspace = true
1818
tracing.workspace = true
19-
typetag.workspace = true

packages/core/shield/src/user.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::fmt::Debug;
33
use chrono::{DateTime, Utc};
44
use serde::{Deserialize, Serialize};
55

6-
#[typetag::serde(tag = "type")]
76
pub trait User: Debug + Send + Sync {
87
fn id(&self) -> String;
98
}
@@ -62,7 +61,6 @@ pub(crate) mod tests {
6261
id: String,
6362
}
6463

65-
#[typetag::serde]
6664
impl User for TestUser {
6765
fn id(&self) -> String {
6866
self.id.clone()

packages/integrations/shield-leptos-actix/src/integration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use leptos::prelude::provide_context;
55
use leptos_actix::{extract, redirect};
66
use shield::{Session, ShieldDyn, User};
77
use shield_actix::{ExtractSession, ExtractShield, ExtractUser};
8-
use shield_leptos::LeptosIntegration;
8+
use shield_leptos::integration::{LeptosIntegration, LeptosUser};
99

1010
pub struct LeptosActixIntegration<U: User>(PhantomData<U>);
1111

@@ -29,10 +29,10 @@ impl<U: User + Clone + 'static> LeptosIntegration for LeptosActixIntegration<U>
2929
session
3030
}
3131

32-
async fn extract_user(&self) -> Option<Arc<dyn User>> {
32+
async fn extract_user(&self) -> Option<LeptosUser> {
3333
let ExtractUser(user) = extract::<ExtractUser<U>>().await.expect("TODO");
3434

35-
user.map(|user| Arc::new(user) as Arc<dyn User>)
35+
user.map(|user| user.into())
3636
}
3737

3838
fn redirect(&self, path: &str) {

packages/integrations/shield-leptos-axum/src/integration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use leptos::prelude::provide_context;
55
use leptos_axum::{extract, redirect};
66
use shield::{Session, ShieldDyn, User};
77
use shield_axum::{ExtractSession, ExtractShield, ExtractUser};
8-
use shield_leptos::LeptosIntegration;
8+
use shield_leptos::integration::{LeptosIntegration, LeptosUser};
99

1010
pub struct LeptosAxumIntegration<U: User>(PhantomData<U>);
1111

@@ -29,10 +29,10 @@ impl<U: User + Clone + 'static> LeptosIntegration for LeptosAxumIntegration<U> {
2929
session
3030
}
3131

32-
async fn extract_user(&self) -> Option<Arc<dyn User>> {
32+
async fn extract_user(&self) -> Option<LeptosUser> {
3333
let ExtractUser(user) = extract::<ExtractUser<U>>().await.expect("TODO");
3434

35-
user.map(|user| Arc::new(user) as Arc<dyn User>)
35+
user.map(|user| user.into())
3636
}
3737

3838
fn redirect(&self, path: &str) {

packages/integrations/shield-leptos/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ version.workspace = true
1111
[dependencies]
1212
async-trait.workspace = true
1313
leptos.workspace = true
14+
serde.workspace = true
1415
shield = { path = "../../core/shield" }
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use std::sync::Arc;
22

33
use leptos::prelude::expect_context;
4-
use shield::{ShieldDyn, User};
4+
use shield::ShieldDyn;
55

6-
use crate::integration::LeptosIntegration;
6+
use crate::integration::{LeptosIntegration, LeptosUser};
77

88
pub fn expect_server_integration() -> Arc<dyn LeptosIntegration> {
99
expect_context::<Arc<dyn LeptosIntegration>>()
1010
}
1111

12-
pub async fn expect_shield() -> ShieldDyn {
12+
pub async fn extract_shield() -> ShieldDyn {
1313
let server_integration = expect_server_integration();
1414
server_integration.extract_shield().await
1515
}
1616

17-
pub async fn extract_user() -> Option<Arc<dyn User>> {
17+
pub async fn extract_user() -> Option<LeptosUser> {
1818
let server_integration = expect_server_integration();
1919
server_integration.extract_user().await
2020
}

0 commit comments

Comments
 (0)