Where can I find any documentation about `RouteDefinition`?
I can see (in
dist/types.d.ts
) what properties it has, but I don't know what some of them are for. Or maybe I'm looking in the wrong place? What I need is to attach a "permission name" to each route, and then before rendering the component check if the logged in user has that permission. Is RouteDefinition.info
the right place for that?46 Replies
info
is the place for that yeahthx
and how would I know that? are there any docs on that?
i don't think so
i just figured it out from playing with it
ah
also fwiw you might be better off splitting the routes into groups and using a layout for each group to do the permission checks, so that you don't have to do it on every route
good point, thx
but how do I do that?
via the
children
property?
and more importantly, how do I access the info
property of the component being rendered?GitHub
GitHub - solidjs/solid-router at a2652b4eab6576db78e6371e5c0aa45eea...
A universal router for Solid inspired by Ember and React Router - GitHub - solidjs/solid-router at a2652b4eab6576db78e6371e5c0aa45eea85d98d
yeah, I've been looking at this for the past 15 minutes or so, but still can't figure out how to use it 😦
for starters, how do I import it?
but how do I do that?Are using vanilla solid-router or SolidStart file routes?
great question! no idea, I suppose the former
(I'm not the original programmer who set up Solid in this project)
definitely vanilla
I tried
import {useCurrentMatches} from '@solidjs/router';
, but it says "module @solidjs.router
has no exported member useCurrentMatches
"If you look at
https://stackblitz.com/edit/stackblitz-starters-p8qprb?file=src%2Fapp.tsx
You'll see
Contract
and Prepare
are nested inside of ContractLayout
which is a layout that looks like this:
In this particular case this was done so that only the two nested routes have access to the particular provider that is shimmed in by the layout.
Similarly checks could be done here to force an immediate Navigate to a less sensitive area in case anything was amiss.peerreynders
StackBlitz
solidjs/router Layout for nested routes - StackBlitz
A Solid TypeScript project based on @solidjs/meta, @solidjs/router, solid-js, typescript, vite and vite-plugin-solid
thanks a lot for your help
it's a pity that it doesn't help me much...
1. We use "configuration-based routing" (https://docs.solidjs.com/solid-router/getting-started/defining-routes#configuration-based-routing), not the
Route
components
(this is probably the least of the problems)
2. I can't see anything about accessing info
here
where should I look?@solidjs.router has no exported member useCurrentMatches"It was only introduced 3 weeks ago so you need 0.13.3
We use "configuration-based routing"Doesn't matter, same idea.
I thought so
ok, thanks
I upgraded
@solidjs.router
, and useCurrentMatches
seems to work, thanksyour routes would look something like this
and the layouts would look something like this
this approach won't need
info
since the layouts do the permission check on behalf of all their childrenI see
interesting
though I might prefer the
info
route (pun not intended) because I have pretty many of those permissionsare you consuming the route
info
in a parent route and handling it there?and I assume that
checkPermission()
should in fact be a different function for each permission, right?
not yet, I'm still figuring out how to get it in the component codeyeah that's up to you
wait a second
how does this tree-like structure work here?
if both the parent and the child (in
routes
above) have the component
property?the children have unique paths
path-less routes are basically invisible to the matching algorithm
which is a whole beast in itself
I have something like that:
and when I'm on
/article
, useCurrentMatches
gives me 2 results instead of one
am I doing this correctly?
(there is nothing about how to do that in the docs, I'm basically doing it via trial and error... 😦 )if both the parent and the child (in routes above) have the component property?The components in a parent will be layout components (taking
props.children
) while only components in leaf routes are terminal.yea the first match is the
/article
layout and the second is the /
leaf route
layout is a bit of a misnomer since you haven't supplied a component there but yeahok, what is a "layout component"? I didn't find that term in the docs
i think the docs refer to them as nested routes
so if I'm deeper in the nested component tree, the leaf and all its parents are the matches?
Correct
So you should get access to the info on each level
I see
that makes sense
although is not ideal in my use-case
since it probably means that I'll need to define my
permissions
on each level
and is it a good idea not to have any component on higher-level routes, only on the leaves?
the first example in https://docs.solidjs.com/solid-router/concepts/nested-routes suggests sooh if it's a good idea not to then call me a sinner haha, layout components are super useful
they're perfectly fine to use dw
This works just fine
yeah, I'm looking at that example right now
and it doesn't make sense to me
Leaf nodes, or innermost Route components, are the only ones that become their own route.what does that mean? and if this: is not the way to go, why does
useCurrentMatches
return anything but the leaf?This would have no definition for
/users
if you want my 2c, these permissions should probably be defined through your data layer rather than in the router. when someone tries to load
/article
have the request that fetches/mutates the article check the user's permissions (either on the client just before the request or during the request on the server), and then act accordingly whether that's a redirect or toast or something. that way you're guaranteed to get the right permissions behaviour no matter where you consume that data.IOW, that example seems to suggest that I shouldn't give any components to the non-leaf routes, no?
It starts by saying "If the parent route needs to be it's own route".
I already handle them on the backend like you say, and return 401 if needed, but I my feeling was that since user without permission X should not be able to do anything with
Article
s, they should not be even allowed to navigate there – and the router seems the obvious place to handle that, correct?
yeah, it says so, and that's exactly what I don't understand – what does it mean "to be its own route"?it probably is the obvious place to yeah but i think doing a fine-grained approach rather than applying route metadata is preferable
maybe in general, yes, but probably not in my use-case
we have an app which has about a dozen "modules"; each module has one or more routes/pages; and each user is permitted to access some set of modules
so each permission maps precisely to a set of routes/pages
- Both
/users
and /users/[id]
are nested inside UsersLayout
- Users
appears inside UsersLayout
for /users
- User
appears inside UsersLayout
for /users/[id]
So Users
and User
are on their own routes while UsersLayout
isn't on it's own route.so, does it mean that this:
means that if someone navigates to
/users/1
, something like this will be rendered?
Conceptually yes while at the same time
/users
isn't defined (no children).GitHub
GitHub - solidjs/solid-router: A universal router for Solid inspire...
A universal router for Solid inspired by Ember and React Router - solidjs/solid-router
yeah, that makes sense
OTOH, I've just encountered something that doesn't
using my previous example
when I'm on
/article
, the /article/:article_id
one seems to be also in the array returned by useCurrentMatches
aaah
now I get it
I get two matches for another reason
one because of the parent (/article
) and one because of the leaf (/
)
correct?Articles
covers /article
so there is nothing amiss here.
Correct