DioxusLabs / dioxus

Fullstack app framework for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
20.22k stars 775 forks source link

Protected/Conditional Routes #1728

Open slyons opened 9 months ago

slyons commented 9 months ago

Similar to Leptos it would be great if there was a way to put Routes behind a condition. If the condition (based on a simple function that can use all the same hooks as Elements) isn't true, the Route is redirected much like the redirect attribute.

ealmloff commented 9 months ago

In Dioxus, parsing the route happens through a FromStr implementation. That is nice for compatibility with the rust ecosystem, but it means you cannot access any non-global hooks inside of any parsing logic.

Adding a guard (which is just a wrapper over a redirect) would be a nice addition. It could just be a redirect that returns Option<Route> and continues to children if it returns None:

#[rustfmt::skip]
#[derive(Clone, Debug, PartialEq, Routable)]
enum Route {
    #[guard("/:id", |id: usize| id.is_even().then(|| Index {})]
        #[route("/")]
        BlogList {
            id: usize
        },
    #[end_guard]
    #[route("/")]
    Index {},
}

Note: this should not be used for anything that needs to be securely hidden. Because everything runs on the frontend, you could get past the guard pretty easily

itsezc commented 8 months ago

@ealmloff Note: this should not be used for anything that needs to be securely hidden. Because everything runs on the frontend, you could get past the guard pretty easily

Would the implementation of #[guard] be any different where fullstack is used?

ealmloff commented 8 months ago

In dioxus fullstack, we could bundle split the protected routes and only serve the route if the guard condition is met to more securely hide content.