BA
Better Auth•4mo ago
Avi0nic

How to wait for new auth data to populate useSession state?

I'm trying to redirect after a login, but the session variables aren't updating soon enough to be used in any of my route guards, who think the user is still logged out. Currently I'm fixing it by adding an await $auth.refetch(), which works, but results in an extra trip to the server, and isn't part of the it's public types (I just happened to stumble on it). Is there anything else I can wait on? I kinda expected to be able to await the signIn.social call and know that the store would be updated too. I've attached a screenshot of my login function which also shows the type error on using refetch
No description
7 Replies
IlBossDiBari
IlBossDiBari•4mo ago
in the docs there is something similar : in the options of the authClient.signIn.social there is a onSuccess property ( or something like that ) u can use to redirect users once logged in. i suppose u can use this
Avi0nic
Avi0nicOP•4mo ago
Thanks, that is one of the things I tried though. I just switched to managing my auth store with Tanstack Query and it works much better.
iatomic_1
iatomic_1•4mo ago
Share your code
Avi0nic
Avi0nicOP•4mo ago
@iatomic.btc I'll try to do that tomorrow at work. Although we are probably just going to stick with Tanstack Query for now, since it allows us to manage caching for offline use bettter. Although if you have some thoughts for that as well, I'm all ears 🙂
Ping
Ping•4mo ago
off-topic: i'm curious, what is $auth in this case?
Avi0nic
Avi0nicOP•4mo ago
It's the store you get back from authClient.useStore() It's basicaly the heart of the issue. Everything uses the store, but since the store doesn't update right away after you called authClient.signIn.social, any checks against it fail if they are made right away.
Chandra
Chandra•4mo ago
If you share the code please ping me

Did you find this page helpful?