Dioxus 0.6 authentication and extractors #3414
Replies: 2 comments 8 replies
-
At the axum level (not sure if this helps or not), you set up middle ware to do the authentication "the way you want"(tm). Example is a bit different from the above example because that uses the This will set an Axum extension using something like: request.extensions_mut().insert(user); And use that value to call inner layers: let mut response = inner.call(request).await?; I think you are using full stack. So presumably you will want the component to have access to the user too. You can do this via a server function: [server(MagicNumber)]
async fn user() -> Result<u32, ServerFnError> {
let Extension(user) = extract::<Extension<user>, _>().await?;
Ok(user)
} And the component can look something like: [component]
fn DisplayUser() -> Element {
let user = use_resource(user);
rsx! {
div {
match &*user.read() {
Some(Ok(user)) => {
rsx! {
p { "User: {u ser}" }
}
}
Some(Err(err)) => {
rsx! {
p { "Error loading your user: {err}." }
}
}
None => {
rsx! {
p { "Loading user..." }
}
}
}
p { "Segments: {segments}" }
}
}
} Disclaimer: Not tested but copied and pasted from code that was working :-). However I just learnt you can use the extract function to retrieve axum extensions. I didn't know that. Cool! |
Beta Was this translation helpful? Give feedback.
-
I have found two issues with authentication: First issue: current method is for the client front end to send a server request to ask what the user is. As in my example above. Which is fine, but when the server generates the pre-rendered page, it has no idea what the user is. So we end up rendering the anonymous user page, and then switching after we get the response to the server request. Which is OK but not ideal. Presumably(???) the solution would be to provide a function to the But: As far as I am aware, this function supplied to Second issue: Expiration of user sessions. e.g. if user logs out, it might be expected that all windows will stop showing sensitive information. Or if the session has timed out. Actually I haven't really thought much about this problem yet. But I think it s an important issue. |
Beta Was this translation helpful? Give feedback.
-
I'm a bit lost on how to use extractors correctly with 0.6 without replicating the whole
launch
function body fromdioxus_fullstack
.The documentation still links to the 0.5 tag: https://github.com/DioxusLabs/dioxus/blob/v0.5/packages/fullstack/examples/axum-auth/src/main.rs
Is there any way to pass layers to the
LaunchBuilder
or do we really need to replicate everything from here for now?Beta Was this translation helpful? Give feedback.
All reactions