Auth in layout?
Is it safe to do auth in layout components? For instance if I have a function like so and call it in the layout component with createAsync will it work on all page changes within that layout? Or is it like Nextjs where it should be called per page?
7 Replies
I think doing in layout components is fine
I tried it by adding it to (main).tsx as the root layout and navigating from / -> /feed. I added a console.debug to the function and it only logs one time. So it seems like if I lost or expired session between navigating it wouldn't catch it.
instead of trying to force a redirect, I use this in
App()
The instant user()
becomes empty, the app displays the Login
component.
User is in a context, and every component calls setUser(checkAuth(user()))
at the top.Thanks, that's an interesting approach. Can you reflect in the url and history that they've moved to a different page like /auth/login if you wanted to?
I haven't tried, I mostly stick to SPAs.
presumably this would work.
https://developer.mozilla.org/en-US/docs/Web/API/History_API/Working_with_the_History_API
This is Solid Start right? I ended up going with a
<ProtectRoute>
component which has an SSR-blocking resource for auth, and conditionally renders a <Navigate>
component once auth is resolved as unauthenticated. Seems to work great with layouts