`Unexpected behavior with 'auth.updateUser({ phone })': Phone linked to incorrect user ID`
Last edited: 8/12/2026
在使用 auth.updateUser({ phone: '...' }) 时,你可能会发现,在手机验证过程中,一个电话号码意外地被链接到不同的 auth.users 记录,而不是当前认证用户,即使 auth.getUser() 之前报告了正确的用户 ID。
🌐 When using auth.updateUser({ phone: '...' }), you might observe that a phone number is unexpectedly linked to a different auth.users record than the currently authenticated user during the phone verification process, even if auth.getUser() reports the correct user ID beforehand.
为什么会发生这种情况?
Supabase 的手机号验证是通过在 phone_change 列中查找提供的手机号来识别用户的,而不是仅仅依赖当前的活跃会话。和 phone 列不同,phone_change 列不强制唯一性。如果多个 auth.users 记录因为未完成或放弃的验证尝试而在 phone_change 中包含相同的手机号,系统在成功的 OTP 验证后可能会更新一个非预期用户的 phone 字段。这是因为系统会找到并更新 phone_change 中的第一个匹配记录,而这个记录可能不属于当前已认证的用户。
如何防止/解决这个问题:
为了防止因放弃的验证尝试导致模糊查找,请在应用层实现清理,移除你 auth.users 记录中陈旧的 phone_change 值。
- 定义宽限期: 设定一个合理的时间段,超过这个时间未确认的手机验证尝试将被视为过期。
- 识别过时记录: 定期查询
auth.users,找出phone_verified为false且phone_change值已存在超过你定义的宽限期的账户。 - 清除
phone_change: 对于已确定的过期记录,清除它们的phone_change值。这样可以确保在验证时只考虑活跃且唯一的phone_change条目。