Replies: 1 comment 2 replies
-
So, this is somewhat tricky because it requires you to defer rendering anything from the server until this The best current way to do this is to put your user check in the route handler itself on the server, and then pass it down into your app as a prop or via context. You can do this with [ App::new()
.route("/api/{tail:.*}", leptos_actix::handle_server_fns())
.leptos_preloaded_data_routes(
leptos_options.to_owned(),
routes.to_owned(),
|req| async move {
if req.path() == "/settings" {
CurrentUser::extract(&req).await.ok()
} else {
None
}
}
|cx, data| {
provide_context(cx, data);
view! { cx, <App/> },
}
) |
Beta Was this translation helpful? Give feedback.
-
I've got this server function that extracts an
Option<CurrentUser>
from an actix request. It looks like this:I would like to make some route redirect if the
Option<CurrentUser>
isNone
. For example, if a user hits/settings
andget_current_user
returnsNone
I'd like them to be redirected to/login
.My initial thought is to write something at the top of a
Settings
component that:None
.loading...
and navigate to a new page when the function resolvesNone
.But my thoughts are, this phase of the components rendering should always only happen on the server because it is for the entire page, and the server knows if there is a current user. So it seems like rather than complicate the settings component, there might be some way to run some code that gates whether the Settings component can be rendered, and if not redirect.
A more general way to describe this problem would be: How do I either render a component or redirect on the server depending on some state that the server holds?
Beta Was this translation helpful? Give feedback.
All reactions