跳转到内容

Android 上的 Apple 登录

Android 上的 Apple 登录比较复杂。Apple 没有官方支持 Android 上的 Sign in with Apple,因此解决方案略显取巧。

Android 目前使用 chrome 标签页显示 OAuth2 网站。这种方法面临以下挑战:

  • 配置困难
  • 需要后端

让我用一张图表来解释 Android 上的流程:

        flowchart TD
            A("await SocialLogin.login()") -->|Handled in the plugin|B(Generate the login URL)
            B --> |Pass the link| C(Open the Chrome browser)
            C --> D(Wait for the user to login)
            D --> |Apple redirects to your backend|E(Handle the data returned from Apple)
            E --> F(Redirect back to the app)
            F --> G(Return to JS)
        

现在你已经了解了挑战和流程,让我们开始配置。

  1. 登录到 Apple Developer Portal

  2. 点击 Identifiers

    Apple Developer Portal Identifiers section

    你应该会看到如下屏幕:

    Apple Developer Portal Identifiers screen
    1. 确保此字段显示 App IDs
    2. 确保你能找到你的 App ID。
  3. 确保为你的应用启用了 Sign in with Apple 功能

    1. 点击你的应用 Selecting your app from the list
    2. 确保已启用 Sign in with Apple 功能 Sign in with Apple capability enabled checkbox
    3. 如果未启用,请启用它。
  4. 返回 All Identifiers

    All Identifiers navigation button
  5. 点击 App Ids 并转到 Services IDs

    Navigation to Services IDs section
  6. 创建新标识符

    1. 点击加号按钮

      Add new service ID button
    2. 选择 Servcice IDs 并点击 Continue

      Selecting Service IDs option
    3. 输入描述和标识符,然后点击 Continuie

      Entering service ID details
    4. 请验证详情并点击 Register

      Confirming service ID registration
    5. 点击新创建的服务

      Selecting newly created service ID
    6. 启用 Sign in with Apple 选项

      Enabling Sign in with Apple for service ID
    7. 配置 Sign In with Apple

      Configure button for Sign in with Apple
    8. 确保 Primary App ID 设置为上一步中配置的 App ID

      Setting Primary App ID dropdown
    9. 添加你将托管后端的域名。

      Setting domain and return URL fields
    10. 确认数据并点击 Done

      Confirming domain and return URL configuration
    11. 点击 Continue

      Continue button for service configuration
    12. 点击 Save

      Save button for service configuration
  1. 返回 All Identifiers

    All Identifiers navigation button
  2. 点击 Keys

    Keys section in Apple Developer Portal
  3. 点击加号图标

    Add new key button
  4. 命名你的密钥

    Entering key name field
  5. 选择 Sign in with Apple 并点击 Configure

    Enabling and configuring Sign in with Apple for the key
  6. 选择主 App ID,然后按 Save

    Selecting primary App ID for the key
  7. 点击 Continue

    Continue button for key configuration
  8. 点击 Register

    Register button for key creation
  9. 复制密钥 ID 并下载密钥。

    Key ID and download button screen
  10. 找到下载的密钥并将其保存在后端文件夹中。

    Downloaded key file

为了在 Android 上使用 Login with Apple,你需要获取 Team ID。它将在后端中使用。

  1. 前往此网站并向下滚动

  2. 找到 Team ID

    Team ID location in developer account

正如你在图表中看到的,后端执行一个名为 Redirect back to the app 的步骤。这需要手动更改你的应用。

  1. 修改 AndroidManifest.xml
    1. 打开文件,我将使用 AndroidStudio

      AndroidManifest.xml file in Android Studio
    2. 找到 MainActivity 并添加以下 Intent filter

      Intent filter code to add in MainActivity
      <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="capgo-demo-app" android:host="path" />
      </intent-filter>
  2. 修改 MainActivity
    1. 请打开 MainActivity

      MainActivity.java file in Android Studio
    2. 添加以下代码:

      Code to add to MainActivity for handling deep links
      @Override
      protected void onNewIntent(Intent intent) {
      String action = intent.getAction();
      Uri data = intent.getData();
      if (Intent.ACTION_VIEW.equals(action) && data != null) {
      PluginHandle pluginHandle = getBridge().getPlugin("SocialLogin");
      if (pluginHandle == null) {
      Log.i("Apple Login Intent", "SocialLogin login handle is null");
      return;
      }
      Plugin plugin = pluginHandle.getInstance();
      if (!(plugin instanceof SocialLoginPlugin)) {
      Log.i("Apple Login Intent", "SocialLogin plugin instance is not SocialLoginPlugin");
      return;
      }
      ((SocialLoginPlugin) plugin).handleAppleLoginIntent(intent);
      return;
      }
      super.onNewIntent(intent);
      }

Android 需要后端,但配置后端也会影响 iOS。此处提供了一个示例后端

此示例提供以下内容:

  • 简单的 JSON 数据库
  • 从 Apple 服务器请求 JWT 的方法
  • 简单的 JWT 验证

根据我在本教程中所说的一切,以下是 env 部分的样子:

  • ANDROID_SERVICE_ID = 服务 ID
  • IOS_SERVICE_ID = App ID
env: {
PRIVATE_KEY_FILE: "AuthKey_U93M8LBQK3.p8",
KEY_ID: "U93M8LBQK3",
TEAM_ID: "UVTJ336J2D",
ANDROID_SERVICE_ID: "ee.forgr.io.ionic.starter.service2",
IOS_SERVICE_ID: "me.wcaleniewolny.test.ionic.vue",
PORT: 3000,
REDIRECT_URI: "https://xyz.wcaleniewolny.me/login/callback",
BASE_REDIRECT_URL: "capgo-demo-app://path"
}

login 函数的使用方式没有变化,与 iOS 相同。有关更多信息,请参阅该部分。但是,initialize 方法有一些变化。

await SocialLogin.initialize({
apple: {
clientId: 'ee.forgr.io.ionic.starter.service2',
redirectUrl: 'https://appleloginvps.wcaleniewolny.me/login/callback'
}
})
  1. 如果你还没有 App ID,请点击加号按钮

    Add new identifier plus button
  2. 选择 App IDs 并点击 continue

    Selecting App IDs type
  3. 点击类型 App 并点击 Continue

    Selecting App type
  4. 输入描述和 App ID

    Entering app description and bundle ID
  5. 启用 Sign with Apple 功能

    Enabling Sign in with Apple capability
  6. 点击 Continue

    Continue button for app registration
  7. 确认详情并点击 Register

    Confirming app registration details