useNavigate outside of router - for example on authentication faiures?
I have a file of functions for interacting with my backend, I'd like to consolidate some error handling there, for example, if there is an authentication failure, redirect.
But as these are not in a router, I can't
useNavigate
, even though all these functions are called from a component under a router.
Is there another strategy I can use here?5 Replies
If you are using solid-start with @solid-router you can:
throw redirect(“yourPath“,config)
No, this is an SPA with just SolidJS.
What is your router?
With the latest solid-start you define if you are using ssr or pure client side rendering in the app.config.js
But you can still use the solid router helpers like redirect.
It will just trigger the redirect on the client router.
solid-router, hash based. But not using start, at all.
My work-around, is in my API module, I throw an error, that my callers in my components can then navigate to the login page on. But its a bit tedious to do it for every API call, when my
post
wrapper for example could just do it.If you use solid router then you can use the redirect helper. It’s part of the solid router package and work for client and ssr apps.
https://docs.solidjs.com/solid-router/reference/data-apis/response-helpers#redirect