diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2d695d6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.rustfmt.overrideCommand": ["/home/felix/.cargo/bin/leptosfmt", "--stdin", "--rustfmt"] +} \ No newline at end of file diff --git a/src/frontend/app.rs b/src/frontend/app.rs index c39548a..429e537 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -1,3 +1,4 @@ +use crate::frontend::components::protected_route::IbisProtectedRoute; use crate::{ common::SiteView, frontend::{ @@ -6,12 +7,8 @@ use crate::{ dark_mode::DarkMode, pages::{ article::{ - actions::ArticleActions, - create::CreateArticle, - edit::EditArticle, - history::ArticleHistory, - list::ListArticles, - read::ReadArticle, + actions::ArticleActions, create::CreateArticle, edit::EditArticle, + history::ArticleHistory, list::ListArticles, read::ReadArticle, }, diff::EditDiff, instance::{details::InstanceDetails, list::ListInstances}, @@ -35,7 +32,8 @@ pub fn site() -> Resource { } pub fn is_logged_in() -> bool { - site().with_default(|site| site.my_profile.is_some()) + //site().with_default(|site| site.my_profile.is_some()) + false } pub fn is_admin() -> bool { site().with_default(|site| { @@ -61,7 +59,7 @@ impl DefaultResource for Resource { } pub fn shell(options: LeptosOptions) -> impl IntoView { view! { - + @@ -101,13 +99,16 @@ pub fn App() -> impl IntoView { - - + + - // TODO: use protected route, otherwise user can view - // /article/create without login - // https://github.com/leptos-rs/leptos/blob/leptos_0.7/examples/router/src/lib.rs#L51 - + @@ -115,7 +116,7 @@ pub fn App() -> impl IntoView { - + diff --git a/src/frontend/components/mod.rs b/src/frontend/components/mod.rs index c8e65ae..6ee98a4 100644 --- a/src/frontend/components/mod.rs +++ b/src/frontend/components/mod.rs @@ -4,3 +4,4 @@ pub mod credentials; pub mod editor; pub mod instance_follow_button; pub mod nav; +pub mod protected_route; \ No newline at end of file diff --git a/src/frontend/components/protected_route.rs b/src/frontend/components/protected_route.rs new file mode 100644 index 0000000..1b4d591 --- /dev/null +++ b/src/frontend/components/protected_route.rs @@ -0,0 +1,28 @@ +use leptos::prelude::*; +use leptos_router::{ + components::{ProtectedRoute, ProtectedRouteProps}, + NestedRoute, SsrMode, +}; +use crate::frontend::app::is_logged_in; + +#[component(transparent)] +pub fn IbisProtectedRoute( + path: Segments, + view: ViewFn, + #[prop(optional)] ssr: SsrMode, +) -> NestedRoute AnyView + Send + Clone> +where + ViewFn: Fn() -> View + Send + Clone + 'static, + View: IntoView + 'static, +{ + let condition = move || Some(is_logged_in()); + let redirect_path = || "/"; + let props = ProtectedRouteProps { + path, + view, + condition, + redirect_path, + ssr, + }; + ProtectedRoute(props) +}