Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return body data when redirect in ServerFn #1513

Closed
Bechma opened this issue Aug 8, 2023 · 4 comments
Closed

Return body data when redirect in ServerFn #1513

Bechma opened this issue Aug 8, 2023 · 4 comments

Comments

@Bechma
Copy link
Contributor

Bechma commented Aug 8, 2023

Is your feature request related to a problem? Please describe.
When we have a server function in axum that redirects to a page, the Ok result is not returned in the redirect response.

https://github.com/leptos-rs/leptos/blob/main/examples/session_auth_axum/src/auth.rs#L174-L175

If instead of returning Ok(()), the return value is a String like Ok(String::from("done")) the response will have a redirect status header but an empty body. So when handled the return value to handle errors, a success login will imply to handle the deserialization, as it will be empty:

#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub enum LoginMessages {
    Successful,
    Unsuccessful,
}

let result_of_call = login.value();

let error = move || {
    result_of_call.with(|msg| {
        msg.as_ref()
            .map(|inner| match inner {
                Ok(LoginMessages::Unsuccessful) => "Incorrect user or password",
                Ok(LoginMessages::Successful) | Err(ServerFnError::Deserialization(_)) => "",
                Err(x) => {
                    tracing::error!("Problem during login: {x:?}");
                    "There was a problem, try again later"
                }
            })
            .unwrap_or_default()
    })
};

Describe the solution you'd like
Return the body data also along with the redirect

Describe alternatives you've considered
I handle as success the deserialization, although I don't think it's a good practice

Additional context
I have a shared LoginAction, so when I navigate away from the Login page and come back, the Deserialization error is there.
https://github.com/Bechma/realworld-leptos/blob/main/src/app.rs#L17
https://github.com/Bechma/realworld-leptos/blob/main/src/routes/login.rs#L15-L16

@gbj
Copy link
Collaborator

gbj commented Aug 8, 2023

I think this is a duplicate of #1358, so reading through the discussion there might be helpful. Unless it's different from this situation, this is determined by the way the browser handles the redirect, not by what the server function itself actually returns as an HTTP response.

@Bechma
Copy link
Contributor Author

Bechma commented Aug 8, 2023

I see in the linked issue how they fix the problem, however, I see too much boilerplate for that matter...
I was thinking into adding a custom approach, so with leptos_axum::redirect you are adding in the requests parts a header, so maybe also we can add a boxed body?

Response::builder()
    .status(StatusCode::FOUND)
    .header(header::LOCATION, "/login")
    .body(axum::body::boxed(/* Encoded response */))
    .unwrap()

Is it good/bad idea?

@gbj
Copy link
Collaborator

gbj commented Aug 8, 2023

Just making sure we're on the same page: as I understand it, the HTTP response returned from a server function that redirects does already include the encoded body. You can see this if you use curl or a similar tool to make the request rather than doing it in the browser. It's just that the fetch request in the browser follows the redirect and does not give any access to that original body.

@Bechma
Copy link
Contributor Author

Bechma commented Aug 8, 2023

Oh! Now I understand... I tried to tune a bit the fetch API but it looks like it's impossible to work along the fetch without using the trick showed in the issue you shared.
I close this issue as it's impossible to fix it without some "js magic"

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

No branches or pull requests

2 participants