dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

Protected/Conditional Routes

Open eadgbear opened this issue 1 year ago • 3 comments

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.

eadgbear avatar Dec 18 '23 18:12 eadgbear

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

ealmloff avatar Dec 18 '23 21:12 ealmloff

@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?

itsezc avatar Dec 28 '23 21:12 itsezc

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.

ealmloff avatar Dec 28 '23 22:12 ealmloff