Skip to content

Commit

Permalink
feat(authentication): expose AdditionalUserInfo (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Jul 23, 2022
1 parent 1b39253 commit 2165073
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-fishes-end.md
@@ -0,0 +1,5 @@
---
"@capacitor-firebase/authentication": patch
---

feat: expose `AdditionalUserInfo`
21 changes: 16 additions & 5 deletions packages/authentication/README.md
Expand Up @@ -60,7 +60,7 @@ These configuration values are available:

| Prop | Type | Description | Default | Since |
| -------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`skipNativeAuth`** | <code>boolean</code> | Configure whether the plugin should skip the native authentication. Only needed if you want to use the Firebase JavaScript SDK. Only available for Android and iOS. | <code>false</code> | 0.1.0 |
| **`skipNativeAuth`** | <code>boolean</code> | Configure whether the plugin should skip the native authentication. Only needed if you want to use the Firebase JavaScript SDK. **Note that the plugin may behave differently across the platforms.** Only available for Android and iOS. | <code>false</code> | 0.1.0 |
| **`providers`** | <code>string[]</code> | Configure which providers you want to use so that only the providers you need are fully initialized. If you do not configure any providers, they will be all initialized. Please note that this does not prevent the automatic initialization of third-party SDKs. Only available for Android and iOS. | <code>["apple.com", "facebook.com", "github.com", "google.com", "microsoft.com", "playgames.google.com", "twitter.com", "yahoo.com", "phone"]</code> | 0.1.0 |

### Examples
Expand Down Expand Up @@ -777,10 +777,11 @@ Remove all listeners for this plugin.

#### SignInResult

| Prop | Type | Description | Since |
| ---------------- | ----------------------------------------------------------------- | --------------------------------------------------------- | ----- |
| **`user`** | <code><a href="#user">User</a> \| null</code> | The currently signed-in user, or null if there isn't any. | 0.1.0 |
| **`credential`** | <code><a href="#authcredential">AuthCredential</a> \| null</code> | Credentials returned by an auth provider. | 0.1.0 |
| Prop | Type | Description | Since |
| ------------------------ | ------------------------------------------------------------------------- | --------------------------------------------------------------- | ----- |
| **`user`** | <code><a href="#user">User</a> \| null</code> | The currently signed-in user, or null if there isn't any. | 0.1.0 |
| **`credential`** | <code><a href="#authcredential">AuthCredential</a> \| null</code> | Credentials returned by an auth provider. | 0.1.0 |
| **`additionalUserInfo`** | <code><a href="#additionaluserinfo">AdditionalUserInfo</a> \| null</code> | Additional user information from a federated identity provider. | 0.5.1 |


#### User
Expand Down Expand Up @@ -809,6 +810,16 @@ Remove all listeners for this plugin.
| **`nonce`** | <code>string</code> | The random string used to make sure that the ID token you get was granted specifically in response to your app's authentication request. | 0.1.0 |


#### AdditionalUserInfo

| Prop | Type | Description | Since |
| ---------------- | ---------------------------------------- | ----------------------------------------------------------- | ----- |
| **`isNewUser`** | <code>boolean</code> | Whether the user is new (sign-up) or existing (sign-in). | 0.5.1 |
| **`profile`** | <code>{ [key: string]: unknown; }</code> | Map containing IDP-specific user data. | 0.5.1 |
| **`providerId`** | <code>string</code> | Identifier for the provider used to authenticate this user. | 0.5.1 |
| **`username`** | <code>string</code> | The username if the provider is GitHub or Twitter. | 0.5.1 |


#### CreateUserWithEmailAndPasswordOptions

| Prop | Type | Since |
Expand Down
Expand Up @@ -8,7 +8,9 @@
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AdditionalUserInfo;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
Expand Down Expand Up @@ -100,7 +102,7 @@ public void createUserWithEmailAndPassword(PluginCall call) {
if (task.isSuccessful()) {
Log.d(FirebaseAuthenticationPlugin.TAG, "createUserWithEmailAndPassword succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null);
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(signInResult);
} else {
Log.e(FirebaseAuthenticationPlugin.TAG, "createUserWithEmailAndPassword failed.", task.getException());
Expand Down Expand Up @@ -222,7 +224,7 @@ public void signInWithCustomToken(final PluginCall call) {
if (task.isSuccessful()) {
Log.d(FirebaseAuthenticationPlugin.TAG, "signInWithCustomToken succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null);
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(signInResult);
} else {
Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithCustomToken failed.", task.getException());
Expand Down Expand Up @@ -250,7 +252,7 @@ public void signInWithEmailAndPassword(final PluginCall call) {
if (task.isSuccessful()) {
Log.d(FirebaseAuthenticationPlugin.TAG, "signInWithEmailAndPassword succeeded.");
FirebaseUser user = getCurrentUser();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null);
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(user, null, null, null, null, null);
call.resolve(signInResult);
} else {
Log.e(FirebaseAuthenticationPlugin.TAG, "signInWithEmailAndPassword failed.", task.getException());
Expand Down Expand Up @@ -320,20 +322,24 @@ public void handleOnActivityResult(int requestCode, int resultCode, Intent data)
}
}

public void handleSuccessfulSignIn(final PluginCall call, AuthCredential credential, String idToken) {
handleSuccessfulSignIn(call, credential, idToken, null, null);
}

public void handleSuccessfulSignIn(
final PluginCall call,
@Nullable AuthCredential credential,
@Nullable String idToken,
@Nullable String nonce,
@Nullable String accessToken
@Nullable String accessToken,
@Nullable AdditionalUserInfo additionalUserInfo
) {
boolean skipNativeAuth = this.config.getSkipNativeAuth();
if (skipNativeAuth) {
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(null, credential, idToken, nonce, accessToken);
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(
null,
credential,
idToken,
nonce,
accessToken,
additionalUserInfo
);
call.resolve(signInResult);
return;
}
Expand All @@ -344,13 +350,14 @@ public void handleSuccessfulSignIn(
task -> {
if (task.isSuccessful()) {
Log.d(FirebaseAuthenticationPlugin.TAG, "signInWithCredential succeeded.");
FirebaseUser user = getCurrentUser();
AuthResult authResult = task.getResult();
JSObject signInResult = FirebaseAuthenticationHelper.createSignInResult(
user,
credential,
authResult.getUser(),
authResult.getCredential(),
idToken,
nonce,
accessToken
accessToken,
authResult.getAdditionalUserInfo()
);
call.resolve(signInResult);
} else {
Expand Down
Expand Up @@ -2,9 +2,11 @@

import androidx.annotation.Nullable;
import com.getcapacitor.JSObject;
import com.google.firebase.auth.AdditionalUserInfo;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.OAuthCredential;
import java.util.Map;

public class FirebaseAuthenticationHelper {

Expand All @@ -13,16 +15,20 @@ public static JSObject createSignInResult(
@Nullable AuthCredential credential,
@Nullable String idToken,
@Nullable String nonce,
@Nullable String accessToken
@Nullable String accessToken,
@Nullable AdditionalUserInfo additionalUserInfo
) {
JSObject userResult = FirebaseAuthenticationHelper.createUserResult(user);
JSObject credentialResult = FirebaseAuthenticationHelper.createCredentialResult(credential, idToken, nonce, accessToken);
JSObject additionalUserInfoResult = FirebaseAuthenticationHelper.createAdditionalUserInfoResult(additionalUserInfo);
JSObject result = new JSObject();
result.put("user", userResult);
result.put("credential", credentialResult);
result.put("additionalUserInfo", additionalUserInfoResult);
return result;
}

@Nullable
public static JSObject createUserResult(@Nullable FirebaseUser user) {
if (user == null) {
return null;
Expand All @@ -40,6 +46,7 @@ public static JSObject createUserResult(@Nullable FirebaseUser user) {
return result;
}

@Nullable
public static JSObject createCredentialResult(
@Nullable AuthCredential credential,
@Nullable String idToken,
Expand Down Expand Up @@ -78,4 +85,27 @@ public static JSObject createCredentialResult(
}
return result;
}

@Nullable
public static JSObject createAdditionalUserInfoResult(@Nullable AdditionalUserInfo additionalUserInfo) {
if (additionalUserInfo == null) {
return null;
}
JSObject result = new JSObject();
result.put("isNewUser", additionalUserInfo.isNewUser());
if (additionalUserInfo.getProfile() != null) {
JSObject profileResult = new JSObject();
for (Map.Entry<String, Object> entry : additionalUserInfo.getProfile().entrySet()) {
profileResult.put(entry.getKey(), entry.getValue());
}
result.put("profile", profileResult);
}
if (additionalUserInfo.getProviderId() != null) {
result.put("providerId", additionalUserInfo.getProviderId());
}
if (additionalUserInfo.getUsername() != null) {
result.put("username", additionalUserInfo.getUsername());
}
return result;
}
}
Expand Up @@ -5,6 +5,7 @@
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AdditionalUserInfo;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.OAuthProvider;
Expand Down Expand Up @@ -86,7 +87,8 @@ private void startActivityForSignIn(final PluginCall call, OAuthProvider.Builder
.addOnSuccessListener(
authResult -> {
AuthCredential credential = authResult.getCredential();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce, null);
AdditionalUserInfo additionalUserInfo = authResult.getAdditionalUserInfo();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce, null, additionalUserInfo);
}
)
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
Expand All @@ -97,7 +99,8 @@ private void finishActivityForSignIn(final PluginCall call, Task<AuthResult> pen
.addOnSuccessListener(
authResult -> {
AuthCredential credential = authResult.getCredential();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce, null);
AdditionalUserInfo additionalUserInfo = authResult.getAdditionalUserInfo();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, currentNonce, null, additionalUserInfo);
}
)
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
Expand Down
Expand Up @@ -97,7 +97,7 @@ private void handleSuccessCallback(LoginResult loginResult) {
if (savedCall == null) {
return;
}
pluginImplementation.handleSuccessfulSignIn(savedCall, credential, null, null, accessTokenString);
pluginImplementation.handleSuccessfulSignIn(savedCall, credential, null, null, accessTokenString, null);
}

private void handleCancelCallback() {
Expand Down
Expand Up @@ -65,7 +65,7 @@ public void handleOnActivityResult(final PluginCall call, ActivityResult result)
pluginImplementation.handleFailedSignIn(call, null, exception);
}

pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, accessToken);
pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, accessToken, null);
}
)
.start();
Expand Down
Expand Up @@ -5,9 +5,9 @@
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AdditionalUserInfo;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.OAuthCredential;
import com.google.firebase.auth.OAuthProvider;
import dev.robingenz.capacitorjs.plugins.firebase.authentication.FirebaseAuthentication;
import dev.robingenz.capacitorjs.plugins.firebase.authentication.FirebaseAuthenticationPlugin;
Expand Down Expand Up @@ -41,7 +41,8 @@ private void startActivityForSignIn(final PluginCall call, OAuthProvider.Builder
.addOnSuccessListener(
authResult -> {
AuthCredential credential = authResult.getCredential();
pluginImplementation.handleSuccessfulSignIn(call, credential, null);
AdditionalUserInfo additionalUserInfo = authResult.getAdditionalUserInfo();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, null, null, additionalUserInfo);
}
)
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
Expand All @@ -52,7 +53,8 @@ private void finishActivityForSignIn(final PluginCall call, Task<AuthResult> pen
.addOnSuccessListener(
authResult -> {
AuthCredential credential = authResult.getCredential();
pluginImplementation.handleSuccessfulSignIn(call, credential, null);
AdditionalUserInfo additionalUserInfo = authResult.getAdditionalUserInfo();
pluginImplementation.handleSuccessfulSignIn(call, credential, null, null, null, additionalUserInfo);
}
)
.addOnFailureListener(exception -> pluginImplementation.handleFailedSignIn(call, null, exception));
Expand Down
Expand Up @@ -44,14 +44,14 @@ private void verifyPhoneNumber(final PluginCall call, String phoneNumber) {

private void handleVerificationCode(final PluginCall call, String verificationId, String verificationCode) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, verificationCode);
pluginImplementation.handleSuccessfulSignIn(call, credential, null);
pluginImplementation.handleSuccessfulSignIn(call, credential, null, null, null, null);
}

private PhoneAuthProvider.OnVerificationStateChangedCallbacks createCallbacks(final PluginCall call) {
return new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
pluginImplementation.handleSuccessfulSignIn(call, credential, null);
pluginImplementation.handleSuccessfulSignIn(call, credential, null, null, null, null);
}

@Override
Expand All @@ -61,7 +61,7 @@ public void onVerificationFailed(FirebaseException exception) {

@Override
public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) {
JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null, null);
JSObject result = FirebaseAuthenticationHelper.createSignInResult(null, null, null, null, null, null);
result.put("verificationId", verificationId);
call.resolve(result);
}
Expand Down
Expand Up @@ -49,7 +49,7 @@ public void handleOnActivityResult(final PluginCall call, ActivityResult result)
String serverAuthCode = account.getServerAuthCode();
AuthCredential credential = PlayGamesAuthProvider.getCredential(serverAuthCode);
String idToken = account.getIdToken();
pluginImplementation.handleSuccessfulSignIn(call, credential, idToken);
pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, null, null);
} catch (ApiException exception) {
pluginImplementation.handleFailedSignIn(call, null, exception);
}
Expand Down

0 comments on commit 2165073

Please sign in to comment.