question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(authentication): expose `AdditionalUserInfo`

See original GitHub issue

Is your feature request related to an issue? Please describe:

Yeah. In social media entries (Facebook, Google, GooglePlay, Twitter, Apple etc.), the user’s id value connected to the provider does not appear. (GoogleId value does not come for login with Google.)

Describe your desired solution:

When the user logs in, the user’s id value can be added according to the response that the provider returns. (I left a small demo below)

Describe the alternatives you are considering:

I mentioned it in the additional context section.

Additional context:

I have attached the codes to be edited. This is for android only. It works successfully when I tested it for Facebook, Google and Twitter.

Thanks for your time!

  1. FirebaseAuthenticationHelper.java

// updated one function


// update params

    public static JSObject createSignInResult(FirebaseUser user, AuthCredential credential, String idToken, String id) {
        JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user);
        JSObject credentialResult = FirebaseAuthenticationHelper.createCredentialResult(credential, idToken); // update call params
        JSObject result = new JSObject();
        
// add next line

       userResult.put("id", id); // add this line
        result.put("user", userResult);
        result.put("credential", credentialResult);
        return result;
    }

  1. FirebaseAuthentication.java

// updated two functions


    public void signInWithCustomToken(PluginCall call) {
        boolean skipNativeAuth = this.config.getSkipNativeAuth();
        if (skipNativeAuth) {
            call.reject(ERROR_CUSTOM_TOKEN_SKIP_NATIVE_AUTH);
            return;
        }

        String token = call.getString("token", "");

        firebaseAuthInstance
            .signInWithCustomToken(token)
            .addOnCompleteListener(
                plugin.getActivity(),
                new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Log.d(FirebaseAuthenticationPlugin.TAG, "signInWithCustomToken succeeded.");
                            FirebaseUser user = getCurrentUser();

// updated next line

JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null); // update call params
                            call.resolve(signInResult);
                        } else {
                            Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithCustomToken failed.", task.getException());
                            call.reject(ERROR_SIGN_IN_FAILED);
                        }
                    }
                }
            )
            .addOnFailureListener(
                plugin.getActivity(),
                new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception) {
                        Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithCustomToken failed.", exception);
                        call.reject(ERROR_SIGN_IN_FAILED);
                    }
                }
            );
    }

// 1. update params    

public void handleSuccessfulSignIn(final PluginCall call, AuthCredential credential, String idToken, String id) {
        boolean skipNativeAuth = this.config.getSkipNativeAuth();
        if (skipNativeAuth) {

// 2. update call params

            JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(null, credential, idToken, id); // update call params
            call.resolve(signInResult);
            return;
        }
        firebaseAuthInstance
            .signInWithCredential(credential)
            .addOnCompleteListener(
                plugin.getActivity(),
                new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Log.d(FirebaseAuthenticationPlugin.TAG, "signInWithCredential succeeded.");
                            FirebaseUser user = getCurrentUser();

// 3. update call params

                            JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, credential, idToken, id); // update call params
                            call.resolve(signInResult);
                        } else {
                            Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithCredential failed.", task.getException());
                            call.reject(ERROR_SIGN_IN_FAILED);
                        }
                    }
                }
            )
            .addOnFailureListener(
                plugin.getActivity(),
                new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithCredential failed.", exception);
                        call.reject(ERROR_SIGN_IN_FAILED);
                    }
                }
            );
    }

  1. handlers/PlayGamesAuthProviderHandler.java

update one Function


    public void handleOnActivityResult(PluginCall call, ActivityResult result) {
        Intent data = result.getData();
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            GoogleSignInAccount account = task.getResult(ApiException.class);
            String serverAuthCode = account.getServerAuthCode();
            AuthCredential credential = PlayGamesAuthProvider.getCredential(serverAuthCode);
            String idToken = account.getIdToken();
            String id = account.getId(); // add this and update call from next line
            pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, id); // update call params
        } catch (ApiException exception) {
            pluginImplementation.handleFailedSignIn(call, null, exception);
        }
    }

  1. handlers/PhoneAuthProviderHandler

// updated three functions


    private void handleVerificationCode(PluginCall call, String verificationId, String verificationCode) {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, verificationCode);

// update call params from next line

        pluginImplementation.handleSuccessfulSignIn(call, credential, null, null); // update call params
    }

            @Override
            public void onVerificationCompleted(PhoneAuthCredential credential) {

// update call params from next line

                pluginImplementation.handleSuccessfulSignIn(call, credential, null, null); // update call params
            }

            @Override
            public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) {

// update call params from next line             

   JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null); // update call params
                result.put("verificationId", verificationId);
                call.resolve(result);
            }

  1. handlers/OAuthProviderHandler

// update two functions


    private void startActivityForSignIn(final PluginCall call, OAuthProvider.Builder provider) {
        pluginImplementation
            .getFirebaseAuthInstance()
            .startActivityForSignInWithProvider(pluginImplementation.getPlugin().getActivity(), provider.build())
            .addOnSuccessListener(
                authResult -> {
                    AuthCredential credential = authResult.getCredential();
                    
// add next line and update call params

Object userId = authResult.getAdditionalUserInfo().getProfile().get("id");
                    pluginImplementation.handleSuccessfulSignIn(call, credential, null, userId.toString()); // update call params
                }
            )
            .addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
    }

    private void finishActivityForSignIn(final PluginCall call, Task<AuthResult> pendingResultTask) {
        pendingResultTask
            .addOnSuccessListener(
                authResult -> {
                    AuthCredential credential = authResult.getCredential();
                    
// add next line and update call params

Object userId = authResult.getAdditionalUserInfo().getProfile().get("id");
                    pluginImplementation.handleSuccessfulSignIn(call, credential, null,  userId.toString()); // update call params
                }
            )
            .addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
    }

  1. handlers/GoogleAuthProviderHandler

update one function


    public void handleOnActivityResult(PluginCall call, ActivityResult result) {
        Intent data = result.getData();
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            GoogleSignInAccount account = task.getResult(ApiException.class);
            String idToken = account.getIdToken();

// add next line and update call params

            String id = account.getId();
            AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
            pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, id); // update call params
        } catch (ApiException exception) {
            pluginImplementation.handleFailedSignIn(call, null, exception);
        }
    }

  1. handlers/FacebookAuthProviderHandler

update one function


    private void handleSuccessCallback(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        String token = accessToken.getToken();

// add next line and update call params     

   String id = accessToken.getUserId();
        AuthCredential credential = FacebookAuthProvider.getCredential(token);
        pluginImplementation.handleSuccessfulSignIn(savedCall, credential, token, id); // update call params
    }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
carsonsuitecommented, Jun 27, 2022

@robingenz I hope exams are going well. What is the estimated lead time on resolving this?

0reactions
robingenzcommented, Jul 23, 2022

You can now test the first dev version:

npm i @capacitor-firebase/authentication@0.5.0-dev.afeddcc.1658580404

Example to get the Google user ID (Android):

const result = await FirebaseAuthentication.signInWithGoogle();
const googleUserId = result?.additionalUserInfo?.profile?.sub;

@typefox09 Unfortunately your problem had nothing to do with AdditionalUserInfo, because the Apple DisplayName is not part of this interface (see Firebase docs). So for your problem I would like to try to find another solution first, before we extend interfaces with custom properties. I have created a new issue for this: #155

Read more comments on GitHub >

github_iconTop Results From Across the Web

feat(authentication): expose AdditionalUserInfo #30 - GitHub
Yeah. In social media entries (Facebook, Google, GooglePlay, Twitter, Apple etc.), the user's id value connected to the provider does not appear ...
Read more >
AdditionalUserInfo | Firebase - Google
public interface AdditionalUserInfo implements Parcelable. Object that contains additional user information as a result of a successful sign-in, link, ...
Read more >
How to get AuthResult in flutter Firebase UI to find ...
1 Answer 1 ... From looking at the code, it looks like the firebase-ui-flutter library that you're using does not expose the user...
Read more >
firebase_auth | Flutter Package - Pub.dev
Flutter plugin for Firebase Auth, enabling Android and iOS authentication using passwords, ... (9ad97c82); FEAT: expose reauthenticateWithRedirect and ...
Read more >
تويتر \ Robin Genz (robin_genz@) - Twitter
Release: Capacitor Firebase v0.5.1 Improve native error handling Authentication: - feat: expose `AdditionalUserInfo` - fix: `customParameters` was not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found