New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass response logic to children routes #74
Comments
I'm not exactly certain what you're trying to do. Maybe if you show me what you're doing right now, it will be more clear. Even some pseudo code of what you would like might help. |
Sure thing. So I have something like this:
So whenever I hit |
Okay, now we're on the same page. I'll look into what other routers/web frameworks do with this.
These two strategies can be distinguished by when they do the authentication check.
In theory, routes could have a check that would prevent their children from attempting to match against a location. For example, an {
name: 'Dashboard',
path: 'dashboard',
on: {
match: () => {
// when the user is not logged in, this will return false
// and the router will skip this tree
return isLoggedIn;
}
},
response: () => {
return {
body: Dashboard,
}
},
children: [
{
name: 'Community',
path: 'community',
response: () => {
return {
body: Community
}
}
},
]
} I think I would prefer just "protecting" each route with a shared function. This might be a little less convenient because you have to add it to every "protected" route, but it makes more sense to me and doesn't add any more complication to the router. function protectedRoute(proceed) {
return function(routeData) {
if (!isLoggedIn) {
return {
redirectTo: {
name: "Login",
state: { next: routeData.match.location.pathname }
}
};
}
return proceed(matchData);
}
}
const routes = [
{
name: 'Dashboard',
path: 'dashboard',
response: protectedRoute(() => {
return {
body: Dashboard,
};
},
children: [
{
name: 'Community',
path: 'community',
response: protectedRoute(() => {
return {
body: Community
}
})
},
]
}
] |
Yeah I figured this might be the way to go. Thank you for confirming! |
Is there a way to pass response logic to children routes? The use case is I have one parent route then a bunch of children routes, say like:
So I would like to block access to
album
and all its children. The problem is, it seems the best I can do create a function and pass it to every child. However, I'm looking to only write and use a piece a logic once in the parent. Any ideas? Thanks.The text was updated successfully, but these errors were encountered: