feat(authentication): expose `AdditionalUserInfo`
See original GitHub issueIs 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!
- 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;
}
- 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);
}
}
);
}
- 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);
}
}
- 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);
}
- 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));
}
- 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);
}
}
- 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:
- Created 2 years ago
- Comments:15 (7 by maintainers)
@robingenz I hope exams are going well. What is the estimated lead time on resolving this?
You can now test the first dev version:
Example to get the Google user ID (Android):
@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