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

更新Supabase自定义URL后,Expo React Native中Apple Sign-In无法返回邮箱或姓名

问题分析与解决方案

核心原因推测

更新Supabase自定义URL后,Apple登录的用户信息(邮箱、姓名)无法正常返回,大概率是Apple开发者后台或Supabase的OAuth配置遗漏关键项,其次是代码未做JWT信息的兜底处理,具体如下:

1. Apple开发者后台配置缺失

  • Email转发未启用:隐藏邮箱时,Apple需要开启「Forward to your domain」才能生成xxxxx@privaterelay.appleid.com中转邮箱,更新URL后可能误关闭该选项,导致邮箱字段为空。
  • Return URLs未同步更新:Apple后台的Sign in with Apple配置中,未将新的Supabase自定义URL回调地址(https://<你的自定义域名>/auth/v1/callback)添加到Return URLs列表,导致Apple拒绝返回敏感权限数据。
  • App ID权限未重新验证:更新域名后,Apple可能需要重新确认应用的权限范围,未完成验证会导致首次登录也无法返回姓名、邮箱。

2. Supabase OAuth配置未同步

Supabase控制台的Apple身份验证提供者配置中,Redirect URI未更新为新的自定义URL回调地址,导致身份验证流程中Apple返回的信息无法被Supabase正确接收,进而影响前端获取数据。

3. 代码未做JWT兜底处理

Apple的用户信息(姓名、邮箱)会同时包含在identityToken的JWT Payload中,但当前代码仅从credential对象获取数据,若credential因配置问题未返回数据,没有备选方案兜底。


分步解决方案

1. 检查并修复Apple开发者后台配置

  1. 进入Apple开发者后台,找到对应App ID的「Sign in with Apple」配置:
    • 确认Domain and Subdomains填写的是新的Supabase自定义域名。
    • 开启「Email Forwarding」,并填写与自定义URL一致的域名。
    • 在「Return URLs」中添加新的Supabase回调地址:https://<你的自定义域名>/auth/v1/callback,同时保留原网页端回调地址(若需要)。
  2. 重新生成并下载Apple的Sign in with Apple配置文件,确保Expo项目使用最新配置。

2. 同步Supabase OAuth配置

在Supabase控制台的「Authentication > Providers > Apple」页面:

  • 更新Redirect URI为新的自定义URL回调地址,确保与Apple后台的Return URLs完全一致。
  • 确认Team ID、Service ID、Key ID、Private Key等核心配置未被误修改。

3. 优化代码,添加JWT兜底逻辑

安装jwt-decode包解析Apple的identityToken,作为credential数据的备选:

import jwtDecode from 'jwt-decode';

const signInWithApple = async () => {
  try {
    const credential = await AppleAuthentication.signInAsync({
      requestedScopes: [
        AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
        AppleAuthentication.AppleAuthenticationScope.EMAIL,
      ],
    });

    const appleIdentityToken = credential.identityToken;
    // 解析JWT获取兜底数据
    const decodedToken = jwtDecode(appleIdentityToken);
    
    // 优先从credential取数据,没有则用JWT中的值
    const firstName = credential.fullName?.givenName || decodedToken.given_name || '';
    const lastName = credential.fullName?.familyName || decodedToken.family_name || '';
    const email = credential.email || decodedToken.email || '';

    const metadata = { firstName, lastName };
    setNameFromApple(metadata);

    if (!appleIdentityToken) {
      return { error: { message: 'Failed to obtain identity token' } };
    }

    const { data, error } = await supabase.auth.signInWithIdToken({
      provider: 'apple',
      token: appleIdentityToken,
      // 可选:将邮箱、姓名作为metadata传给Supabase
      options: {
        data: { email, firstName, lastName }
      }
    });

    if (error) {
      return { error };
    }

    if (data) {
      setSession(data.session);
      if (data.session) setUser(data?.session?.user);
      return { data };
    }
  } catch (error) {
    return { error };
  }
};

4. 清除缓存并重新测试

  • iOS设备:进入「设置 > [你的Apple ID] > 密码与安全性 > 使用Apple ID的App」,找到你的应用并选择「停止使用Apple ID」,然后重新登录测试。
  • Expo项目:运行expo r -c清理缓存并重启,或重新构建原生包(expo build:ios)确保配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 15:25:23