dpa99c / cordova-plugin-firebasex

Cordova plugin for Google Firebase
MIT License
570 stars 457 forks source link

Add idToken on signIn w/ Apple response #857

Closed TheNotorius0 closed 4 months ago

TheNotorius0 commented 4 months ago

Feature request

There is already an opened feature request about this (https://github.com/dpa99c/cordova-plugin-firebasex/issues/551) but since it says fixed by https://github.com/dpa99c/cordova-plugin-firebasex/pull/633 , maybe it has been forgotten.

I think that a lot of us use both the cordova-plugin-firebasex and the Firebase javascript SDK for different things. For example I use this plugin for push notifications, while I use the Firebase javascript SDK for Firestore and the Realtime database (which isn't available using this plugin).

Obviously I need an authenticated user on the Firebase javascript SDK to use the Realtime database, and with Android it has been implemented:

FirebasePlugin.authenticateUserWithGoogle(clientId, function (credential) {

    const credential_web = firebase.auth.GoogleAuthProvider.credential(credential.idToken);

    firebase.auth().signInWithCredential(credential_web).then((result) => {

        showToastShort('Logged-in with Google');
    });
});

But on iOS (authenticateUserWithApple) the credential object returned doesn't have the property idToken, so we can't pass it to the signInWithCredentialfunction. Is it possible to add that property to authenticateUserWithAppleas well? (see: https://github.com/dpa99c/cordova-plugin-firebasex/pull/633)

dpa99c commented 4 months ago

I've exposed the idToken value in the credential result returned by authenticateUserWithApple on the iOS platform in the commit above.

However it's not possible to return the idToken value in the credential result returned by authenticateUserWithApple on the Android platform, since this uses the oAuth provider flow and so the idToken is not generated/available until signInWithCredential has been called to initiate the oAuth sign-in flow during which the idToken is created. Once the sign-in process has succeeded, the idToken that was used is available in the data returned by getCurrentUser.

TheNotorius0 commented 3 months ago

So, I've tried using signInWithCredential after signing-in with Apple with the following code:

            FirebasePlugin.authenticateUserWithApple(function (credential) {

                const appleProvider = new firebase.auth.OAuthProvider('apple.com');
                const appleWebCredentials = appleProvider.credential({idToken: credential.idToken});

                firebase.auth().signInWithCredential(appleWebCredentials).then((result) => {

                    showToastShort('Logged-in with Apple');
                });
            });

Unfortunately, it gives me this error:

FirebaseError: Firebase: Nonce is missing in the request. (auth/missing-or-invalid-nonce).

Is it possible to have the rawNonce property too in the credential object? Thanks

EDIT: I made a test modifying the plugin, and indeed the sign-in now works, passing the rawNonce. This is the change:

[result setValue:rawNonce forKey:@"rawNonce"]; on line 527, under [result setValue:idToken forKey:@"idToken"];

The Cordova updated and working code is:

const appleWebCredentials = appleProvider.credential({idToken: credential.idToken, rawNonce: credential.rawNonce});