1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-29 04:41:09 +00:00

protected route

This commit is contained in:
Felix Ableitner 2024-11-21 13:28:36 +01:00
parent 53fce0db39
commit f022c56a69
4 changed files with 48 additions and 15 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"rust-analyzer.rustfmt.overrideCommand": ["/home/felix/.cargo/bin/leptosfmt", "--stdin", "--rustfmt"]
}

View file

@ -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<SiteView> {
}
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| {
@ -101,13 +99,16 @@ pub fn App() -> impl IntoView {
<Route path=path!("/") view=ReadArticle />
<Route path=path!("/article/:title") view=ReadArticle />
<Route path=path!("/article/:title/history") view=ArticleHistory />
<Route path=path!("/article/:title/edit/:conflict_id?") view=EditArticle />
<Route path=path!("/article/:title/actions") view=ArticleActions />
<IbisProtectedRoute
path=path!("/article/:title/edit/:conflict_id?")
view=EditArticle
/>
<IbisProtectedRoute
path=path!("/article/:title/actions")
view=ArticleActions
/>
<Route path=path!("/article/:title/diff/:hash") view=EditDiff />
// 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
<Route path=path!("/create-article") view=CreateArticle />
<IbisProtectedRoute path=path!("/create-article") view=CreateArticle />
<Route path=path!("/articles") view=ListArticles />
<Route path=path!("/instances") view=ListInstances />
<Route path=path!("/instance/:hostname") view=InstanceDetails />
@ -115,7 +116,7 @@ pub fn App() -> impl IntoView {
<Route path=path!("/login") view=Login />
<Route path=path!("/register") view=Register />
<Route path=path!("/search") view=Search />
<Route path=path!("/notifications") view=Notifications />
<IbisProtectedRoute path=path!("/notifications") view=Notifications />
</Routes>
</main>
</Router>

View file

@ -4,3 +4,4 @@ pub mod credentials;
pub mod editor;
pub mod instance_follow_button;
pub mod nav;
pub mod protected_route;

View file

@ -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<Segments, ViewFn, View>(
path: Segments,
view: ViewFn,
#[prop(optional)] ssr: SsrMode,
) -> NestedRoute<Segments, (), (), impl Fn() -> 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)
}