This has a bug, can you spot it?
async function setUserStore(newUser: User | null) {
console.log("Updating user state", newUser);
user.value = newUser;
if (newUser) {
const fetchedProfile = await useProfileService().fetchProfile(newUser);
setProfileStore(fetchedProfile);
} else {
setProfileStore({});
}
}
Bug:
When the user changes very quickly, and supabase's response times are slow, then this can happen:
- User changes to Jane
- We start fetching the profile (!!!)
- Jane logs out, thus the user changes to null
- We set the profile to empty
- Supabase returns the profile that we tried to fetch a while ago...and now we have a logged out user, with the profile of Jane
This has a bug, can you spot it?
Bug:
When the user changes very quickly, and supabase's response times are slow, then this can happen: