How to Sign in with Apple in Jetpack Compose?

164 views Asked by At

I am trying to implement singing in with Apple in my Jetpack Compose app, through Firebase. I have already setup everything in Firebase etc. since I also use the same firebase server to let users Sign in with Apple in the corresponding iOS app. (Services ID, Apple Team ID, Key ID, Private Key and auth URL)

Here is my code for singing in:

class ContinueWithApple(
    private val context: Context,
    private val appLanguage: String,
    private val isSigningIn: MutableState<Boolean>
): Register()  {

    fun singIn() {

        isSigningIn.value = true

        userIsAlreadySigningIn(auth, isSigningIn) {

            if (!it) {

                singInUser(
                    auth = auth,
                    context = context,
                    appLanguage = appLanguage,
                    isSigningIn = isSigningIn
                ) {

                }

            } //: END IF

        } //: USER IS ALREADY SIGNING IN

    } //: FUN

} //: CLASS

fun userIsAlreadySigningIn(
    auth: FirebaseAuth,
    isSigningIn: MutableState<Boolean>,
    completion: (Boolean) -> Unit
) {

    val pending = auth.pendingAuthResult

    if (pending != null) {

        pending

            .addOnSuccessListener { authResult ->

                Log.d("SIGN IN WITH APPLE", "checkPending:onSuccess:$authResult")
                isSigningIn.value = false
                completion(true)

            } //: ON SUCCESS

            .addOnFailureListener { e ->

                Log.w("SIGN IN WITH APPLE", "checkPending:onFailure", e)
                isSigningIn.value = false
                completion(true)

            } //: ON FAILURE

    } else {

        Log.d("SIGN IN WITH APPLE", "pending: null")
        completion(false)

    } //: END IF

} //: FUN

fun singInUser(
    auth: FirebaseAuth,
    context: Context,
    appLanguage: String,
    isSigningIn: MutableState<Boolean>,
    completion: (FirebaseUser?) -> Unit
) {

    val provider = OAuthProvider
        .newBuilder("apple.com")
        .setScopes(mutableListOf("email", "name"))
        .addCustomParameter("locale", HelperFunctions.currentLanguageLocale(appLanguage))
        .build()

    auth
        .startActivityForSignInWithProvider(context.getActivity(), provider)
        .addOnSuccessListener { authResult ->

            Log.d("SIGN IN WITH APPLE", "activitySignIn:onSuccess:${authResult.user}")
            completion(authResult.user)

        } //: ON SUCCESS

        .addOnFailureListener { e ->

            Log.w("SIGN IN WITH APPLE", "activitySignIn:onFailure", e)
            isSigningIn.value = false
            completion(null)

        } //: ON FAILURE

} //: FUN

When the user presses the button to sing in, a google chrome tab appears, but this error is visible:

{"error":{"code":403,"message":"Requests from this Android client application are blocked.","errors":[{"message":"Requests from this Android client application are blocked.","domain":"global","reason":"forbidden"}],"status":"PERMISSION_DENIED","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_ANDROID_APP_BLOCKED","domain":"googleapis.com","metadata":{"service":"identitytoolkit.googleapis.com","consumer":"projects/952288320256"}}]}}

Any ideas what might be the problem?

P.S. This is how I get the Activity:

internal fun Context.getActivity(): Activity {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }
    throw IllegalStateException("Permissions should be called in the context of an Activity")
}
1

There are 1 answers

2
tomerpacific On

If you look at the documentation, you can see that the error you are getting is because:

The request is denied because it violates API key Android application restrictions.

If you look at this link, it will show you the steps you need to take in order to not receive this error.

I would verify that you have followed all the steps correctly.

Double check that your google-service.json file is correct and that you have added your signed APK SHA-1 as well.