728x90

안드로이드에서 애플 로그인 하는 방법에 대해서 소개한 블로그들이 없어서 내가 직접 작성하고 있는 중

- 우선 안드로이드 같은 경우 redirect URL을 지정해야 콜백을 받을 수 있다(웹뷰로 실행되기 때문에)

- 반면에 아이폰에서는 webAuthenticationOptions 해당 옵션 설정 없이도 잘 동작합니다.(아이폰 친화적)

- 코드는 괭장히 심플하며, clientId만 애플 계정에서 생성한 서비스ID로  넣어주면 된다.

Future<Result<UserData>> login() async {
    try {
      final credential = await SignInWithApple.getAppleIDCredential(
        scopes: [
          AppleIDAuthorizationScopes.email,
          AppleIDAuthorizationScopes.fullName,
        ],
        webAuthenticationOptions: WebAuthenticationOptions(
          clientId: '[애플 개발자 계정에서 생성한 Service ID]',
          redirectUri: Uri.parse('$BASE_URL/user/login/apple/callback'),
        ),
      );


      print(credential);

      final oauthCredential = OAuthProvider("apple.com").credential(
          idToken: credential.identityToken,
          accessToken: credential.authorizationCode);

      // Sign in the user with Firebase. If the nonce we generated earlier does
      // not match the nonce in `appleCredential.identityToken`, sign in will fail.
      final user = await FirebaseAuth.instance.signInWithCredential(oauthCredential);

      Log.i(oauthCredential.toString());

      return Result.success(_getUserData(user));
    } catch(e) {
      Log.e(e.toString());
      return const Result.error(ResponseError(status: '0', message: "Login fail..."));
    }
  }

하지만 여기서 궁금한것은 redirectUrl을 어떻게 구현하였는가가 궁금할 것이다!

아래는 서버쪽에서도 해당 코드를 작성한 내용이니 참고 바란다.

 

 

[Springboot] 안드로이드에서 애플 로그인 Callback 처리 - Apple login in springboot

애플로그인 콜백 처리를 위한 스프링 코드를 공유한다. 아무곳에서도 찾을 수 없어 직접 개발하였다. @CrossOrigin(origins = "https://appleid.apple.com") @PostMapping(value = "/user/login/apple/callback",..

shutupdev.tistory.com

안드로이드 코드내 XML파일에 앱 콜백 스킴을 추가한다.

<!-- Set up the Sign in with Apple activity, such that it's callable from the browser-redirect -->
<activity
android:name="com.aboutyou.dart_packages.sign_in_with_apple.SignInWithAppleCallback"
android:exported="true"
>
    <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="abcsigninwithapple" />
        <data android:path="callback" />
    </intent-filter>
</activity>

<!-- Don't delete the meta-data below.
 This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />

서버쪽에서 설정한 callback scheme를 설정한다

여기서는 abcsigninwithapple으로 설정하였다.

이부분은 꼭 바꿔서 사용하길 추천한다.

복사했습니다!