Skip to content

Conversation

@dprkh
Copy link

@dprkh dprkh commented Dec 16, 2025

This is a breaking change, because it adds a new variant to the ServerFnError enum.

I used AI to generate this to unblock myself, so feel free to reject, but if it is within your vision to have this feature, I am open on working on this further.

Thanks.

@dprkh dprkh requested a review from a team as a code owner December 16, 2025 19:03
@dprkh dprkh force-pushed the fullstack-extractor-redirect branch from 60196c8 to c6ec025 Compare December 16, 2025 19:10
@dprkh
Copy link
Author

dprkh commented Dec 16, 2025

Here is what my code looks like with this feature, so that you know the motivation behind this.

use crate::{extensions, types::user_session};

use dioxus::{
    fullstack::{Cookie, HeaderMap, HeaderValue, extract::FromRequestParts, headers::HeaderMapExt},
    prelude::StatusCode,
    server::http::{header::LOCATION, request::Parts},
};

const SESSION_COOKIE_NAME: &'static str = "session";

impl<S> FromRequestParts<S> for user_session::Id
where
    S: Send + Sync,
{
    type Rejection = (StatusCode, HeaderMap);

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        let Some(auth) = parts.extensions.get::<extensions::Auth>() else {
            tracing::error!("extension `auth` is not initialized");

            return Err((StatusCode::INTERNAL_SERVER_ERROR, HeaderMap::default()));
        };

        let redirect = |to: &'static str| {
            return (StatusCode::TEMPORARY_REDIRECT, HeaderMap::from_iter([(LOCATION, HeaderValue::from_static(to))].into_iter()));
        };

        let Some(cookie) = parts.headers.typed_get::<Cookie>() else {
            return Err(redirect("/sign-up"));
        };

        let Some(session_str) = cookie.get(SESSION_COOKIE_NAME) else {
            return Err(redirect("/sign-up"));
        };

        let Some((data_str, tag_str)) = session_str.split_once('.') else {
            return Err(redirect("/sign-in"));
        };

        if !auth.verify(data_str.as_bytes(), tag_str.as_bytes()) {
            return Err(redirect("/sign-in"));
        }

        let Ok(session_id) = user_session::Id::try_from(data_str.to_string()) else {
            return Err(redirect("/sign-in"));
        };

        Ok(session_id)
    }
}

Without this change, dioxus would treat this as an error, and redirect would not work.

@jkelleyrtp
Copy link
Member

We're not currently merging breaking PRs right now; however, if you wanted to get it into current stable, we could just reuse the ServerError type using the redirect status code, and then attach the required details. The two variants seem similar in structure that this might be able to work.

@dprkh dprkh force-pushed the fullstack-extractor-redirect branch 3 times, most recently from 25abfa3 to 2179a97 Compare December 16, 2025 23:29
@dprkh dprkh force-pushed the fullstack-extractor-redirect branch from 2179a97 to 1ebfba0 Compare December 17, 2025 00:15
@dprkh
Copy link
Author

dprkh commented Dec 17, 2025

Hi @jkelleyrtp,

Thank you for taking a look at my PR.

I made changes accordingly.

Let me know if there's anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants