You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何为Supabase已认证用户手动添加登录提供商?

在Supabase中手动关联用户登录提供商的实现方法

先看Firebase中的实现方式:

在Firebase中,可通过以下代码为当前用户关联登录提供商:

final credential = GoogleAuthProvider.credential(idToken: idToken);
// 或其他提供商
final credential = EmailAuthProvider.credential(email: emailAddress, password: password);
// 为当前用户添加提供商
FirebaseAuth.instance.currentUser?.linkWithCredential(credential);

针对你提到的两个场景,Supabase的手动关联实现方法如下:

场景1:为电话/OAuth注册的用户添加邮箱密码登录方式

  • 确保用户处于登录状态
  • 直接调用linkIdentity方法关联邮箱密码提供商:
    await supabase.auth.linkIdentity(AuthCredential.emailPassword(
      email: "目标邮箱地址",
      password: "设置的密码",
    ));
    
  • 如果用户之前未绑定邮箱,可按以下步骤操作:
    • 设置邮箱:
      await supabase.auth.updateUser(UserAttributes(email: "目标邮箱地址"));
      
    • 发送邮箱验证邮件(推荐,确保邮箱归属):
      await supabase.auth.sendOtp(email: "目标邮箱地址", type: OtpType.emailChange);
      
    • 验证邮箱后设置密码:
      await supabase.auth.updateUser(UserAttributes(password: "设置的密码"));
      

场景2:将OAuth/电话认证关联至现有邮箱密码账号

  • 用户保持邮箱密码登录状态
  • 关联OAuth提供商:调用signInWithOAuth时传入linkIdentity: true参数,完成关联:
    await supabase.auth.signInWithOAuth(
      Provider.google, // 替换为需要的OAuth提供商,如facebook、github等
      options: OAuthOptions(linkIdentity: true),
    );
    
  • 关联电话认证:
    • 发送验证码到目标手机号:
      await supabase.auth.signInWithPhone("+1234567890");
      
    • 输入收到的验证码后完成关联:
      await supabase.auth.linkIdentity(AuthCredential.sms(
        phone: "+1234567890",
        token: "收到的验证码",
      ));
      

补充说明:Supabase默认会自动匹配相同邮箱的OAuth用户到现有账号,但手动关联时必须确保用户处于登录状态,通过上述方法即可完成不同登录提供商的绑定。

内容的提问来源于stack exchange,提问作者Globe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:46:08