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

(ionic-capacitor) 集成Apple Login返回token但用户数据为空问题

问题描述

基于Ionic Cordova构建的应用迁移至Ionic Capacitor架构后,接入Apple登录功能出现异常:Apple服务可正常返回token,但邮箱(email)、姓氏(familyName)、名字(givenName)等所有用户数据字段均返回null。

对应实现代码
openAppleSignIn() {
    const { SignInWithApple } = Plugins;
    SignInWithApple.Authorize()
      .then(async (res) => {
        const respuesta =JSON.stringify(res.response)
        
          let obje = JSON.parse(respuesta)
          //alert(obje.identityToken)
       //alert(res.response.email)
          //alert(JSON.stringify(obje.email))
          
        
        if (obje && obje.identityToken) {
          this.user = obje;

          this.server.providerLogin({profile:{email:obje.email,first_name:obje.givenName,last_name:obje.familyName,name:obje.givenName,given_name:obje.givenName,family_name:obje.familyName}}, 'Google').subscribe((response2:any) => {
            alert(JSON.stringify({profile:{email:obje.email,first_name:obje.givenName,last_name:obje.familyName,name:obje.givenName,given_name:obje.givenName,family_name:obje.familyName}}))
            if(response2.msg != "done")
            {
              this.presentToast(response2.msg, 'warning');
            }
            else
            {
              if(response2.user_id){
                localStorage.setItem('user_id',response2.user_id);
                this.events.publish('user_login', response2.user_id);
                this.presentToast("Bienvenue !", 'secondary');
                this.nav.back();
              }
              else{
                this.presentToast("Une erreur s'est porduite. Merci de bien vouloir réessayer.", 'danger');
              }
            }
    
          });
          
          //console.log('este es el user',this.user)
        } else {
          this.presentAlert();
        }
      })
      .catch((response) => {
        this.presentAlert();
      });
  }
问题原因与处理方案
  • 该现象是Apple登录的官方机制限制,和Capacitor架构迁移、插件代码错误无关。Apple仅会在用户首次对当前应用授权Apple登录时,在授权响应中返回邮箱、姓氏、名字这类个人信息。
  • 只要用户此前已经为同Bundle ID的应用(包括迁移前Cordova架构的旧版本应用)授权过Apple登录,后续无论是重复触发授权、卸载重装应用、更换设备登录,Apple都不会再返回上述个人字段,统一返回null。
  • 测试场景下如果需要验证字段返回逻辑,可先到iOS系统「设置」-「Apple ID」-「密码与安全性」-「使用Apple ID的App」列表中找到你的应用,选择「停止使用Apple ID」撤销原有授权,之后重新触发登录,首次授权时即可拿到完整的用户信息字段。
  • 业务逻辑层面需要注意:必须在用户首次授权成功的当下,就将获取到的邮箱、姓名等信息存储到本地或自有服务端,后续登录流程只能通过返回的identityToken做身份校验,无法重复拉取用户个人信息。
  • 额外代码逻辑提示:当前代码中调用providerLogin接口时传入的登录类型参数硬编码为'Google',属于笔误,需要调整为Apple登录对应的类型标识,避免后续服务端鉴权逻辑出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 05:12:18