Best approach to having protected routes in SolidJs?
Hello, I'm working on securing certain routes in my application to ensure they are protected if a user is not logged in. There are about 10-15 pages that need to be restricted. I'm currently using Better-Auth for authentication and have switched to client-side rendering only for now.
9 Replies
Also I am using an external API, and using axios to fetch data
Typically you want to create a route layout which verifies authentication and redirects to
/login
when not authenticated while all the protected routes are nested within the “protected” layout.Auth - SolidDocs
Documentation for SolidJS, the signals-powered UI framework

Middleware - SolidDocs
Documentation for SolidJS, the signals-powered UI framework
Hi @peerreynders ,
Thank you for this suggested approach of using Route group layout to manage protected pages. I have implemented as you recommended however I running into a small issue: I'm using
Better-Auth
, so the getSession()
requires a context but essentially the Header
passed as argument. See below:
So, which route API in SolidStart provides the required Headers
? Currently, RoutePreloadFunc
, RouteSectionProps
and RouteDefinition
doesn't.Have a look at getRequestEvent
https://docs.solidjs.com/reference/server-utilities/get-request-event#request
getRequestEvent -
Documentation for SolidJS, the signals-powered UI framework
Thank you, @Madaxen86 . Yea, it did it for me.
I'm most grateful!