S
SolidJS5d ago
ish

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
Madaxen86
Madaxen865d ago
If you are using solid-start with @solid-router you can: throw redirect(“yourPath“,config)
ish
ish5d ago
No, this is an SPA with just SolidJS.
Madaxen86
Madaxen865d ago
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.
ish
ish5d ago
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.
Madaxen86
Madaxen865d ago
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