How can I stop code from executing before?
I am using
@solidjs/router
for routing, here's the code:
The Guard
component checks for authentication (whether key
is not in localStorage
) in createEffect
and navigates based on the result. The Root
component is a protected component and only authenticated user can access it (localStorage
contains the key
item).
The Root
component looks something like this:
However, the WebSocket
is connected before the Guard
component validates and navigates the user.3 Replies
Why not create an Authenticated component that only renders its children if the user is authenticated?
Example?
Your current structure renders
<Route><Guard/><Route><Root/></Route></Route>
, but what you want is <Route><Guard><Root/></Guard><Route>
.
So in the JSX, you'd put in <Route path="/" component={<Guard><Root/></Guard>} />
and in Guard, you'd use
<Show when={authenticated()} fallback={<Navigate ...>}>{props.children}</Show>