Apple login on Android
Este contenido aún no está disponible en tu idioma.
Apple login on android is hacky. Apple has no official support for Sign in with Apple on Android, so the solution is slightly hacky.
Android currently uses a chrome tabs to display an OAuth2 website. This approach has the challanges:
- Difficult configuration
- A backend is required
Understanding the flow on android.
Let me use a diagram to explain the flow on 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)
         Now that you are aware of the challlanges and the flow, let’s begin the configuration.
Creating the service ID
- 
Login into the Apple Developer Portal. 
- 
Click on Identifiers. You should see a screen that looks like this:  - Ensure that this field says App IDs
- Make sure that you can find your App ID.
 
- Ensure that this field says 
- 
Make sure that the Sign in with Applecapability is enabled for your app- Click on your app
 
- Ensure that the Sign in with Applecapability is enabled 
- If it isn’t enabled, enable it.
 
- Click on your app
- 
Go back to all All Identifiers 
- 
Click on App Idsand go toServices IDs 
- 
Creare a new identifier - 
Click on the plus button  
- 
Select Servcice IDsand clickContinue 
- 
Enter a description and a identifiers and click Continuie. 
- 
Please verify the details and click Register 
- 
Click on the the newly created service  
- 
Enable the Sign in with Appleoption 
- 
Configure the Sign In with Apple 
- 
Ensure that the Primary App IDis set to the App ID configured in the previous step 
- 
Add the domain that you are going to host you backend on.  
- 
Confirm the data and click Done 
- 
Click on Continue 
- 
Click on Save 
 
- 
Creating the key
- 
Go back to all All Identifiers 
- 
Click on Keys 
- 
Click on the plus icon  
- 
Name your key  
- 
Select Sign in with Appleand clickConfigure 
- 
Select the primary App ID, and press Save 
- 
Click on Continue 
- 
Click on Register 
- 
Copy the key ID and download the key.  
- 
Find the downloaded key and save it in the backend folder.  
Getting the Team ID
In order to use Login with Apple on Android, you need to get the Team ID. It will be used in the backend.
- 
Go to this website and scroll down 
- 
Find the Team ID 
Configuring the app redirect
As you saw in the diagram, the backend performs a step called Redirect back to the app. This requires manual changes to your app.
- Modify the AndroidManifest.xml- 
Open the file, I will use AndroidStuido 
- 
Find the MainActivityand add the following 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> <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>
 
- 
- Modify the MainActivity- 
Please open the MainActivity 
- 
Add the following 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);} @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);}
 
- 
Backend configuration
A backend is required for Android, but configuring a backend will also impact IOS. An example backend is provided here
This example provides the following:
- A simple JSON database
- A way to request the JWT from Apple’s servers
- A simple JWT verification
Given everything that I said in this tutorial, here is how the env section would look:
- ANDROID_SERVICE_ID= Service 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"}Using the plugin
The usage of the login function doesn’t change, it’s the same as IOS. Please take a look at that section for more info. HOWEVER, the initialize method changes a bit.
await SocialLogin.initialize({  apple: {    clientId: 'ee.forgr.io.ionic.starter.service2',    redirectUrl: 'https://appleloginvps.wcaleniewolny.me/login/callback'  }})Creating the app
- 
If you don’t already have an App ID, click on the plus button  
- 
Select App IDsand click continue 
- 
Click on type Appand clickContinue 
- 
Enter the description and the app ID  
- 
Enable Sign with Applecapability 
- 
Click Continue 
- 
Confirm the details and click Register