Replies: 2 comments 2 replies
-
Instead of using React context, read the auth data in the loaders, create a helper like Then if while using the auth data you receive a 401, handle that by throwing a redirect, any thrown response will be followed by RR so in that case you can redirect the user to your login, or if you have a refresh token you can refresh the access token and retry. Then if you need the same data in your UI you can return it from a route loader. |
Beta Was this translation helpful? Give feedback.
-
I don't want to do a redirect, just show a message that the page is accessible only when authenticated instead of the page contents, but also not having to write auth checking logic on every page which uses it. Does it mean I'll have to write a pair of a route component and a loader factory functions to implement that? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I tend to have constructs like this in react codebases:
Auth is a great example for this because its state cannot be derived from URL alone and better be initiated client-side because it is decided reactively against server responses. But any client-dependent logic which pertains to the state of the client (i.e.
localStorage
, IndexedDB, anything worker-related, anything formatting) will be written the same. It kinda works well because the default way of handling forms in react is imperative (therefore strictly client-side) and thus can be handled by calling functions from context.But the with loaders/actions this falls apart, since they don't have access to client context. Even basic things like an api call requiring auth returning
401
, which most likely means loss of credentials mid-request, becomes pretty hard to handle reactively when it happens in loader/action.I've looked up
useOutletContext
as something related, but it doesn't look like it helps with client state changing in the middle of a loader/action and merely solves the problem of routes depending on context data.Beta Was this translation helpful? Give feedback.
All reactions