콘텐츠로 건너뛰기

Apple login on Android

이 콘텐츠는 아직 귀하의 언어로 제공되지 않습니다.

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

  1. Login into the Apple Developer Portal.

  2. Click on Identifiers.

    Apple Developer Portal Identifiers section

    You should see a screen that looks like this:

    Apple Developer Portal Identifiers screen
    1. Ensure that this field says App IDs
    2. Make sure that you can find your App ID.
  3. Make sure that the Sign in with Apple capability is enabled for your app

    1. Click on your app Selecting your app from the list
    2. Ensure that the Sign in with Apple capability is enabled Sign in with Apple capability enabled checkbox
    3. If it isn’t enabled, enable it.
  4. Go back to all All Identifiers

    All Identifiers navigation button
  5. Click on App Ids and go to Services IDs

    Navigation to Services IDs section
  6. Creare a new identifier

    1. Click on the plus button

      Add new service ID button
    2. Select Servcice IDs and click Continue

      Selecting Service IDs option
    3. Enter a description and a identifiers and click Continuie.

      Entering service ID details
    4. Please verify the details and click Register

      Confirming service ID registration
    5. Click on the the newly created service

      Selecting newly created service ID
    6. Enable the Sign in with Apple option

      Enabling Sign in with Apple for service ID
    7. Configure the Sign In with Apple

      Configure button for Sign in with Apple
    8. Ensure that the Primary App ID is set to the App ID configured in the previous step

      Setting Primary App ID dropdown
    9. Add the domain that you are going to host you backend on.

      Setting domain and return URL fields
    10. Confirm the data and click Done

      Confirming domain and return URL configuration
    11. Click on Continue

      Continue button for service configuration
    12. Click on Save

      Save button for service configuration

Creating the key

  1. Go back to all All Identifiers

    All Identifiers navigation button
  2. Click on Keys

    Keys section in Apple Developer Portal
  3. Click on the plus icon

    Add new key button
  4. Name your key

    Entering key name field
  5. Select Sign in with Apple and click Configure

    Enabling and configuring Sign in with Apple for the key
  6. Select the primary App ID, and press Save

    Selecting primary App ID for the key
  7. Click on Continue

    Continue button for key configuration
  8. Click on Register

    Register button for key creation
  9. Copy the key ID and download the key.

    Key ID and download button screen
  10. Find the downloaded key and save it in the backend folder.

    Downloaded key file

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.

  1. Go to this website and scroll down

  2. Find the Team ID

    Team ID location in developer account

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.

  1. Modify the AndroidManifest.xml
    1. Open the file, I will use AndroidStuido

      AndroidManifest.xml file in Android Studio
    2. Find the MainActivity and add the following 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. Modify the MainActivity
    1. Please open the MainActivity

      MainActivity.java file in Android Studio
    2. Add the following code:

      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);
      }

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

  1. If you don’t already have an App ID, click on the plus button

    Add new identifier plus button
  2. Select App IDs and click continue

    Selecting App IDs type
  3. Click on type App and click Continue

    Selecting App type
  4. Enter the description and the app ID

    Entering app description and bundle ID
  5. Enable Sign with Apple capability

    Enabling Sign in with Apple capability
  6. Click Continue

    Continue button for app registration
  7. Confirm the details and click Register

    Confirming app registration details