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

React Native Firebase集成Apple SSO无法获取用户名求助

React Native Firebase集成Apple SSO无法获取用户名称问题解决

问题详情

React Native Firebase集成Apple SSO时,登录功能正常,但用户的displayName始终为null。已确认使用的是新账户,且完成以下操作:

  • 在Firebase控制台认证模块删除了Apple用户邮箱
  • 在Apple ID管理页面停止该应用的Apple登录,私有中转邮箱已在Firebase中匹配删除

相关代码

import auth from '@react-native-firebase/auth';
import { appleAuth } from '@invertase/react-native-apple-authentication';

async function onAppleButtonPress() {
  const appleAuthRequestResponse = await appleAuth.performRequest({
    requestedOperation: appleAuth.Operation.LOGIN,
    requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
  });

  if (!appleAuthRequestResponse.identityToken) {
    throw new Error('Apple Sign-In failed - no identify token returned');
  }

  const { identityToken, nonce } = appleAuthRequestResponse;
  const appleCredential = auth.AppleAuthProvider.credential(identityToken, nonce);

  const res = await auth().signInWithCredential(appleCredential);

  console.log('res', res) //  <-- see next code block

  // below, displayname is null, i do see firebase Id, and the private email relay address.
  console.log('auth().currentUser?', auth().currentUser )

  return res
}

返回结果

// res response
{
  "additionalUserInfo": {
    "profile": {
      "is_private_email": "true",
      "sub": "000630.afe8c07332204888...",
      "exp": 1666292000,
      "real_user_status": 2,
      "iss": "https://appleid.apple.com",
      "c_hash": "yXmD5j_37t1F...",
      "email_verified": "true",
      "aud": "com.mydemo.app",
      "iat": 1666205600,
      "email": "kgszm...@privaterelay.appleid.com",        
      "auth_time": 1666205600,        
      "nonce": "77e6cd4bfca826abc77a179f15b0a8...",        
      "nonce_supported": true      
    },      
    "username": null,      
    "providerId": "apple.com",      
    "isNewUser": true             <---- shows its a new user
  },    
  "user": {      
    "isAnonymous": false,      
    "emailVerified": true,      
    "providerData": [        
      {          
        "providerId": "apple.com",          
        "uid": "000630.afe8c0733220488889977a5c9...",          
        "email": "kgszm...@privaterelay.appleid.com",          
      }      ],      
      "uid": "4VpWSNUxVAeNzuEu4IQrLKq5Mw43",      
      "email": "kgszm...@privaterelay.appleid.com",          
      "refreshToken": "AOEOulaKPm6HzbT....",      
      "displayName": null,             <--- why is this null?
      "tenantId": null,      
      "phoneNumber": null,      
      "photoURL": null,      
      "metadata": {        
      "creationTime": 1666205601897,        
      "lastSignInTime": 1666205601897      
    },      
    "providerId": "firebase"    
    }  
  }

依赖版本

@react-native-firebase/auth@12.9.3


解决方法

1. 直接从Apple授权响应中获取全名

Apple仅在用户首次授权登录时返回fullName字段,后续登录不会重复返回。需直接从appleAuthRequestResponse中提取该信息,而非依赖Firebase自动同步:

async function onAppleButtonPress() {
  const appleAuthRequestResponse = await appleAuth.performRequest({
    requestedOperation: appleAuth.Operation.LOGIN,
    requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
  });

  // 提取Apple返回的全名信息
  const { fullName } = appleAuthRequestResponse;
  let displayName = null;
  if (fullName) {
    displayName = `${fullName.givenName || ''} ${fullName.familyName || ''}`.trim();
  }

  // ... 原有登录逻辑
}

2. 手动更新Firebase用户的displayName

获取到全名后,调用Firebase的updateProfile方法手动更新用户信息,确保displayName被持久化:

const res = await auth().signInWithCredential(appleCredential);
const { fullName } = appleAuthRequestResponse;

// 仅当首次登录且获取到全名时更新
if (fullName && res.user && res.additionalUserInfo?.isNewUser) {
  const displayName = `${fullName.givenName || ''} ${fullName.familyName || ''}`.trim();
  await res.user.updateProfile({ displayName });
  
  // 更新后重新获取用户信息即可看到displayName
  console.log('更新后的用户信息:', auth().currentUser);
}

3. 注意Apple授权的限制

  • 即便用户删除应用授权后重新登录,Apple也可能因缓存策略不再返回全名,建议首次登录时就将姓名存储到你的业务数据库,避免依赖后续登录获取。
  • 如果用户首次登录时拒绝了姓名授权,fullName会为null,这种情况需引导用户手动输入姓名。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 17:01:09