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!
- FirebaseAuthenticationHelper.java
// updated one function
public static JSObject createSignInResult(FirebaseUser user, AuthCredential credential, String idToken, String id) {
JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user);
JSObject credentialResult = FirebaseAuthenticationHelper.createCredentialResult(credential, idToken);
JSObject result = new JSObject();
userResult.put("id", id);
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();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null);
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);
}
}
);
}
public void handleSuccessfulSignIn(final PluginCall call, AuthCredential credential, String idToken, String id) {
boolean skipNativeAuth = this.config.getSkipNativeAuth();
if (skipNativeAuth) {
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(null, credential, idToken, id);
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();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, credential, idToken, id);
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();
pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, id);
} 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);
pluginImplementation.handleSuccessfulSignIn(call, credential, null, null);
}
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
pluginImplementation.handleSuccessfulSignIn(call, credential, null, null);
}
@Override
public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) {
JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null);
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();
Object userId = authResult.getAdditionalUserInfo().getProfile().get("id");
pluginImplementation.handleSuccessfulSignIn(call, credential, null, userId.toString());
}
)
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
}
private void finishActivityForSignIn(final PluginCall call, Task<AuthResult> pendingResultTask) {
pendingResultTask
.addOnSuccessListener(
authResult -> {
AuthCredential credential = authResult.getCredential();
Object userId = authResult.getAdditionalUserInfo().getProfile().get("id");
pluginImplementation.handleSuccessfulSignIn(call, credential, null, userId.toString());
}
)
.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();
String id = account.getId();
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, id);
} 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();
String id = accessToken.getUserId();
AuthCredential credential = FacebookAuthProvider.getCredential(token);
pluginImplementation.handleSuccessfulSignIn(savedCall, credential, token, id);
}
feature priority: medium package: authentication