Android 端 Apple 登录
复制一条配置 prompt,包含安装步骤和该插件的完整 Markdown 指南。
在 Android 上做 Apple 登录很 hacky。Apple 官方并不支持 Sign in with Apple 在 Android 上,因此该方案略显 hack。
Android 目前使用 Chrome tabs 来展示 OAuth2 网页。这种方式存在以下挑战:
- 配置复杂
- 需要后端支持
了解 Android 端的流程。
标题为“理解 Android 上的流程”。让我用流程图来解释 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)
既然你已经了解了挑战和流程,让我们开始配置吧。
创建 Service ID
标题为“创建 Service ID”-
点击
Identifiers.
你应该会看到如下界面:
- 请确保该字段显示为
App IDs - 请确认你能找到你的 App ID。
- 请确保该字段显示为
-
请确保
Sign in with Apple已为您的 App 启用该功能- 点击您的 App
- 请确保
Sign in with Apple已启用该功能
- 如果尚未启用,请将其启用。
- 点击您的 App
-
返回所有
All Identifiers
-
点击
App Ids并前往Services IDs
-
创建新的标识符
-
点击加号按钮
-
选择
Servcice IDs并点击Continue
-
输入描述和标识符并点击
Continuie.
-
请核对详情并点击
Register
-
点击新创建的 Service
-
启用
Sign in with Apple选项
-
配置
Sign In with Apple
-
确保
Primary App ID已设置为上一步配置的 App ID
-
添加您将用于托管后端的域名。
-
确认数据并点击
Done
-
点击
Continue
-
点击
Save
-
创建密钥
“创建密钥”章节-
返回全部
All Identifiers
-
点击
Keys
-
点击加号图标
-
为您的密钥命名
-
选择
Sign in with Apple并点击Configure
-
选择主 App ID,然后点击
Save
-
点击
Continue
-
点击
Register
-
复制 Key ID 并下载密钥。
-
找到下载的密钥并将其保存到后端文件夹中。
获取 Team ID
“获取 Team ID”章节为了使用 Login with Apple 在 Android 上,你需要获取 Team ID。它将用于后端。
-
前往 该网站 并向下滚动
-
找到
Team ID
配置 App 重定向
“配置应用重定向”章节正如你在图中看到的,后端会执行一个名为 Redirect back to the app的操作。这需要手动修改你的 App。
- 修改
AndroidManifest.xml-
打开文件,我会用
AndroidStudio
-
找到
MainActivity并添加以下 Intent filter
<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>
-
- 修改
MainActivity-
请打开
MainActivity
-
添加以下 code:
@Overrideprotected 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= Service IDIOS_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"}使用该 plugin
标题为“Using the plugin”的部分使用 login 函数的使用方式保持不变,与 iOS 一致。详情请参阅该章节。 但是,该 initialize 方法略有不同。
await SocialLogin.initialize({ apple: { clientId: 'ee.forgr.io.ionic.starter.service2', redirectUrl: 'https://appleloginvps.wcaleniewolny.me/login/callback' }})创建应用
标题为“创建 App”的章节-
如果你还没有 App ID,请点击加号按钮
-
选择
App IDs并点击继续
-
点击类型
App并点击Continue
-
输入描述和 App ID
-
启用
Sign with AppleCapability
-
点击
Continue
-
确认详情并点击
Register
在 Android 上继续 Apple 登录
“在 Android 上继续使用 Apple 登录”章节如果你正在使用 Android 端 Apple 登录 规划身份验证和账户流程,并将它与 使用 @capgo/capacitor-social-login 了解使用 @capgo/capacitor-social-login 时的原生能力, @capgo/capacitor-social-login 了解 @capgo/capacitor-social-login 中的实现细节, @capgo/capacitor-passkey 了解 @capgo/capacitor-passkey 中的实现细节, @capgo/capacitor-native-biometric 了解 @capgo/capacitor-native-biometric 中的实现细节,以及 双重身份验证 了解双因素认证中的实现细节。