Unexpected behavior with 'auth.updateUser({ phone })': Phone linked to incorrect user ID
#44990
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
This is a copy of a troubleshooting article on Supabase's docs site. It may be missing some details from the original. View the original article.
When using
auth.updateUser({ phone: '...' }), you might observe that a phone number is unexpectedly linked to a differentauth.usersrecord than the currently authenticated user during the phone verification process, even ifauth.getUser()reports the correct user ID beforehand.Why does this happen?
Supabase phone verification identifies the user by searching for the provided phone number in the
phone_changecolumn, rather than relying solely on the active session. Unlike thephonecolumn, thephone_changecolumn does not enforce uniqueness. If multipleauth.usersrecords contain the same phone number inphone_changedue to uncompleted or abandoned verification attempts, the system may update an unintended user'sphonefield upon successful OTP verification. This occurs because the system finds and updates the first matching record inphone_change, which might not belong to the currently authenticated user.How to prevent/resolve this:
To prevent ambiguous lookups from abandoned verification attempts, implement application-level cleanup to remove stale
phone_changevalues from yourauth.usersrecords.auth.usersto find accounts wherephone_verifiedisfalseand thephone_changevalue has been present beyond your defined grace period.phone_change: For identified stale records, clear theirphone_changevalue. This ensures that only active and uniquephone_changeentries are considered during verification.All reactions