Cookie based authentication approach
Hello all. I have a cookie based authentication approach. In my login page and in my _app layout i have a
beforeLoad
function like this:
login.tsx
_app.tsx
Is there a better approach on this using tanstack query & tanstack router?5 Replies
genetic-orange•12mo ago
"better" in which way?
quickest-silverOP•12mo ago
maybe passing the auth state in context? using suspense? i mean a cleaner (not better sorry) way or a "by the book" approach
evident-indigo•7mo ago
@Manuel Schiller do You think @Zzyzx approach is valid? I'm trying to make 'context' approach and here's what I have:
main.tsx:
what do You think?
genetic-orange•7mo ago
there is no right or wrong here
it totally depends on what you are using
you can of course use query to fetch and cache
or you can store your state in a react context and feed it into router
evident-indigo•7mo ago
Ahh I think I understand, so if i'll go for 'fetch and cache /me' for example then @Zzyzx approach is totally fine
Hi @Manuel Schiller actually i have a question to @Zzyzx approach.
As You can see in beforeLoad i'm using try/catch block because of
queryClient.ensureQueryData
and if statement in catch
block to ensure the error comes from router api, not request. Do You think is that okay? I mean if it's not breaking any rules or something?