Problem When Updating the User in the Session

I set up Theo’s uploadthing to handle my image uploads. On the uploadthing server, I update the user’s image in the database like in the code below. I did not use authClient.updateUser to update the user's image in the database. Now, every time I have a successful update, I check my database and see that the user’s image is updated correctly. My only problem is that the content of the user in the session authClient.userSession does not update—it seems there is about a minute delay before the session is updated. I don't know if it’s just my internet acting up or if I simply don't know how to handle this kind of situation. When there is a change in the database, the session on the client side should be updated immediately so that when I call router.refresh(), the image is displayed right away.
No description
No description
Solution:
This is not your fault! Essentially, useSession will only update whenever a relevant endpoint was called from the client which would have something to do with modifying the user session. Since you're updating the user's image from your server, the BetterAuthClient doesn't have a way of knowing that - thus it doesn't update. ...
Jump to solution
5 Replies
Solution
Ping
Ping2w ago
This is not your fault! Essentially, useSession will only update whenever a relevant endpoint was called from the client which would have something to do with modifying the user session. Since you're updating the user's image from your server, the BetterAuthClient doesn't have a way of knowing that - thus it doesn't update. You would need to find a solution for your front-end to detect that the img was changed, then call getSession yourself.
craftzcode
craftzcodeOP2w ago
so the best thing to do is to use authClient.updateUser? instead of updating the user direct on the db instance?
Ping
Ping2w ago
In your case, it would seem that's the easiest solution, so yeah.
craftzcode
craftzcodeOP2w ago
can we solve this by calling the refetch() of authClient.useSession?
Ping
Ping2w ago
Yeah, it should.

Did you find this page helpful?