1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-29 04:31:10 +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::{ use crate::{
common::SiteView, common::SiteView,
frontend::{ frontend::{
@ -6,12 +7,8 @@ use crate::{
dark_mode::DarkMode, dark_mode::DarkMode,
pages::{ pages::{
article::{ article::{
actions::ArticleActions, actions::ArticleActions, create::CreateArticle, edit::EditArticle,
create::CreateArticle, history::ArticleHistory, list::ListArticles, read::ReadArticle,
edit::EditArticle,
history::ArticleHistory,
list::ListArticles,
read::ReadArticle,
}, },
diff::EditDiff, diff::EditDiff,
instance::{details::InstanceDetails, list::ListInstances}, instance::{details::InstanceDetails, list::ListInstances},
@ -35,7 +32,8 @@ pub fn site() -> Resource<SiteView> {
} }
pub fn is_logged_in() -> bool { 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 { pub fn is_admin() -> bool {
site().with_default(|site| { site().with_default(|site| {
@ -61,7 +59,7 @@ impl<T: Default + Send + Sync> DefaultResource<T> for Resource<T> {
} }
pub fn shell(options: LeptosOptions) -> impl IntoView { pub fn shell(options: LeptosOptions) -> impl IntoView {
view! { view! {
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
@ -101,13 +99,16 @@ pub fn App() -> impl IntoView {
<Route path=path!("/") view=ReadArticle /> <Route path=path!("/") view=ReadArticle />
<Route path=path!("/article/:title") view=ReadArticle /> <Route path=path!("/article/:title") view=ReadArticle />
<Route path=path!("/article/:title/history") view=ArticleHistory /> <Route path=path!("/article/:title/history") view=ArticleHistory />
<Route path=path!("/article/:title/edit/:conflict_id?") view=EditArticle /> <IbisProtectedRoute
<Route path=path!("/article/:title/actions") view=ArticleActions /> 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 /> <Route path=path!("/article/:title/diff/:hash") view=EditDiff />
// TODO: use protected route, otherwise user can view <IbisProtectedRoute path=path!("/create-article") view=CreateArticle />
// /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 />
<Route path=path!("/articles") view=ListArticles /> <Route path=path!("/articles") view=ListArticles />
<Route path=path!("/instances") view=ListInstances /> <Route path=path!("/instances") view=ListInstances />
<Route path=path!("/instance/:hostname") view=InstanceDetails /> <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!("/login") view=Login />
<Route path=path!("/register") view=Register /> <Route path=path!("/register") view=Register />
<Route path=path!("/search") view=Search /> <Route path=path!("/search") view=Search />
<Route path=path!("/notifications") view=Notifications /> <IbisProtectedRoute path=path!("/notifications") view=Notifications />
</Routes> </Routes>
</main> </main>
</Router> </Router>

View file

@ -4,3 +4,4 @@ pub mod credentials;
pub mod editor; pub mod editor;
pub mod instance_follow_button; pub mod instance_follow_button;
pub mod nav; 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)
}