Android 端 Apple 登录
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
在 Android 上实现 Apple 登录比较 hacky。Apple 没有官方支持 Sign in with Apple 在 Android 上,所以这个方案略显 hacky。
Android 目前使用 Chrome 标签页显示 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
章节标题为“创建服务 ID”-
登录 Apple 开发者门户.
-
点击
Identifiers.
您应该看到如下界面:
- 确保该字段显示为
App IDs - 请确认您能找到您的 App ID。
- 确保该字段显示为
-
请确保
Sign in with Apple你的应用已启用该 capability- 点击你的应用
- 确保
Sign in with Applecapability 已启用
- 如果未启用,请启用它。
- 点击你的应用
-
返回全部
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
-
复制密钥 ID 并下载密钥。
-
找到下载的密钥,并将其保存在后端文件夹中。
获取 Team ID
“获取 Team ID”章节为了使用 Login with Apple 在 Android 上,你需要获取 Team ID。它将用于后端。
-
前往 此网站 并向下滚动
-
找到
Team ID
配置 App 重定向
标题为“配置应用重定向”的章节正如你在图中看到的,后端会执行一个名为 Redirect back to the 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= 服务 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"}使用插件
“使用插件”部分……的用法 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 Apple功能
-
点击
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 中的实现细节, 双因素认证 有关双重身份验证的实现细节。