Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Jan 24, 2025
1 parent 80fb9b0 commit 55941df
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 54 deletions.
7 changes: 7 additions & 0 deletions packages/authentication/BREAKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ This is a comprehensive list of the breaking changes introduced in the major ver

## Versions

- [Version 7.x.x](#version-6xx)
- [Version 6.x.x](#version-6xx)
- [Version 5.x.x](#version-5xx)
- [Version 1.x.x](#version-1xx)
- [Version 0.4.x](#version-04x)

## Version 7.x.x

### Google Sign-In

On **Android**, the `accessToken` and `serverAuthCode` are now only requested when the `scopes` option is set.

## Version 6.x.x

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
import static io.capawesome.capacitorjs.plugins.firebase.authentication.FirebaseAuthenticationPlugin.ERROR_NO_USER_SIGNED_IN;
import static io.capawesome.capacitorjs.plugins.firebase.authentication.FirebaseAuthenticationPlugin.TAG;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.IntentSenderRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
import com.getcapacitor.PluginCall;
import com.google.android.gms.auth.api.identity.AuthorizationResult;
import com.google.android.gms.auth.api.identity.Identity;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.ActionCodeSettings;
import com.google.firebase.auth.AdditionalUserInfo;
Expand All @@ -20,7 +27,6 @@
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.UserProfileChangeRequest;
import io.capawesome.capacitorjs.plugins.firebase.authentication.FirebaseAuthenticationHelper.ProviderId;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.ConfirmVerificationCodeOptions;
Expand All @@ -41,10 +47,8 @@
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.OAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.PhoneAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.PlayGamesAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.EmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.NonEmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.Result;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.ResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.EmptyErrorCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.NonEmptyErrorCallback;
import java.util.Arrays;
import java.util.List;
import org.json.JSONObject;
Expand All @@ -58,6 +62,7 @@ public class FirebaseAuthentication {
private AppleAuthProviderHandler appleAuthProviderHandler;
private FacebookAuthProviderHandler facebookAuthProviderHandler;
private GoogleAuthProviderHandler googleAuthProviderHandler;
public ActivityResultLauncher<IntentSenderRequest> googleAuthorizationResultLauncher;
private OAuthProviderHandler oAuthProviderHandler;
private PhoneAuthProviderHandler phoneAuthProviderHandler;
private PlayGamesAuthProviderHandler playGamesAuthProviderHandler;
Expand Down Expand Up @@ -135,7 +140,7 @@ public void confirmPasswordReset(@NonNull String oobCode, @NonNull String newPas
);
}

public void confirmVerificationCode(@NonNull ConfirmVerificationCodeOptions options, @NonNull NonEmptyResultCallback callback) {
public void confirmVerificationCode(@NonNull ConfirmVerificationCodeOptions options, @NonNull NonEmptyErrorCallback callback) {
phoneAuthProviderHandler.confirmVerificationCode(options, callback);
}

Expand All @@ -149,7 +154,7 @@ public void deleteUser(FirebaseUser user, @NonNull Runnable callback) {
);
}

public void fetchSignInMethodsForEmail(FetchSignInMethodsForEmailOptions options, @NonNull final NonEmptyResultCallback callback) {
public void fetchSignInMethodsForEmail(FetchSignInMethodsForEmailOptions options, @NonNull final NonEmptyErrorCallback callback) {
String email = options.getEmail();

getFirebaseAuthInstance()
Expand All @@ -173,7 +178,7 @@ public FirebaseUser getCurrentUser() {
return getFirebaseAuthInstance().getCurrentUser();
}

public void getIdToken(Boolean forceRefresh, @NonNull final NonEmptyResultCallback callback) {
public void getIdToken(Boolean forceRefresh, @NonNull final NonEmptyErrorCallback callback) {
FirebaseUser user = getCurrentUser();
if (user == null) {
callback.error(new Exception(ERROR_NO_USER_SIGNED_IN));
Expand Down Expand Up @@ -327,7 +332,7 @@ public void reload(FirebaseUser user, @NonNull Runnable callback) {
);
}

public void revokeAccessToken(@NonNull RevokeAccessTokenOptions options, @NonNull EmptyResultCallback callback) {
public void revokeAccessToken(@NonNull RevokeAccessTokenOptions options, @NonNull EmptyErrorCallback callback) {
String token = options.getToken();

getFirebaseAuthInstance()
Expand All @@ -336,7 +341,7 @@ public void revokeAccessToken(@NonNull RevokeAccessTokenOptions options, @NonNul
.addOnFailureListener(exception -> callback.error(exception));
}

public void sendEmailVerification(@NonNull SendEmailVerificationOptions options, @NonNull EmptyResultCallback callback) {
public void sendEmailVerification(@NonNull SendEmailVerificationOptions options, @NonNull EmptyErrorCallback callback) {
ActionCodeSettings actionCodeSettings = options.getActionCodeSettings();

FirebaseUser user = getCurrentUser();
Expand All @@ -355,7 +360,7 @@ public void sendEmailVerification(@NonNull SendEmailVerificationOptions options,
task.addOnSuccessListener(unused -> callback.success()).addOnFailureListener(exception -> callback.error(exception));
}

public void sendPasswordResetEmail(@NonNull SendPasswordResetEmailOptions options, @NonNull EmptyResultCallback callback) {
public void sendPasswordResetEmail(@NonNull SendPasswordResetEmailOptions options, @NonNull EmptyErrorCallback callback) {
String email = options.getEmail();
ActionCodeSettings actionCodeSettings = options.getActionCodeSettings();

Expand Down Expand Up @@ -597,7 +602,7 @@ public void verifyBeforeUpdateEmail(
FirebaseUser user,
@NonNull String newEmail,
@NonNull ActionCodeSettings actionCodeSettings,
@NonNull EmptyResultCallback callback
@NonNull EmptyErrorCallback callback
) {
user
.verifyBeforeUpdateEmail(newEmail, actionCodeSettings)
Expand Down Expand Up @@ -663,7 +668,7 @@ public void handleOnActivityResult(int requestCode, int resultCode, @NonNull Int
public void signInWithCredential(
@NonNull SignInOptions options,
@NonNull AuthCredential credential,
@NonNull NonEmptyResultCallback callback
@NonNull NonEmptyErrorCallback callback
) {
boolean skipNativeAuth = options.getSkipNativeAuth();
if (skipNativeAuth) {
Expand All @@ -689,7 +694,7 @@ public void signInWithCredential(
);
}

public void linkWithCredential(@NonNull AuthCredential credential, @NonNull NonEmptyResultCallback callback) {
public void linkWithCredential(@NonNull AuthCredential credential, @NonNull NonEmptyErrorCallback callback) {
FirebaseUser user = getFirebaseAuthInstance().getCurrentUser();
if (user == null) {
callback.error(new Exception(ERROR_NO_USER_SIGNED_IN));
Expand Down Expand Up @@ -882,7 +887,7 @@ public void handleSuccessfulLink(final PluginCall call) {
call.resolve(linkResult);
}

public void handleFailedLink(final PluginCall call, String message, Exception exception) {
public void handleFailedLink(final PluginCall call, @Nullable String message, Exception exception) {
if (message == null && exception != null) {
message = exception.getMessage();
}
Expand Down Expand Up @@ -916,7 +921,7 @@ public FirebaseAuthenticationConfig getConfig() {
}

private void initAuthProviderHandlers(FirebaseAuthenticationConfig config) {
List providerList = Arrays.asList(config.getProviders());
List<String> providerList = Arrays.asList(config.getProviders());
if (providerList.contains(ProviderId.APPLE)) {
appleAuthProviderHandler = new AppleAuthProviderHandler(this);
}
Expand All @@ -925,6 +930,27 @@ private void initAuthProviderHandlers(FirebaseAuthenticationConfig config) {
}
if (providerList.contains(ProviderId.GOOGLE)) {
googleAuthProviderHandler = new GoogleAuthProviderHandler(this);
googleAuthorizationResultLauncher =
getPlugin()
.getActivity()
.registerForActivityResult(
new ActivityResultContracts.StartIntentSenderForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
try {
AuthorizationResult authorizationResult = Identity
.getAuthorizationClient(getPlugin().getActivity())
.getAuthorizationResultFromIntent(intent);
googleAuthProviderHandler.handleAuthorizationResult(authorizationResult);
} catch (ApiException exception) {
googleAuthProviderHandler.handleAuthorizationResultError(exception);
}
} else {
googleAuthProviderHandler.handleAuthorizationResultError(new Exception("Authorization canceled."));
}
}
);
}
if (providerList.contains(ProviderId.PHONE)) {
phoneAuthProviderHandler = new PhoneAuthProviderHandler(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.capawesome.capacitorjs.plugins.firebase.authentication;

import android.app.Activity;
import android.content.Intent;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.IntentSenderRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.getcapacitor.JSObject;
Expand All @@ -11,6 +15,9 @@
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.ActivityCallback;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.google.android.gms.auth.api.identity.AuthorizationResult;
import com.google.android.gms.auth.api.identity.Identity;
import com.google.android.gms.common.api.ApiException;
import com.google.firebase.auth.ActionCodeSettings;
import com.google.firebase.auth.FirebaseUser;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.ConfirmVerificationCodeOptions;
Expand All @@ -22,8 +29,8 @@
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.options.SendEmailVerificationOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.options.SendPasswordResetEmailOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.FacebookAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.EmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.NonEmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.EmptyErrorCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.NonEmptyErrorCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.Result;
import org.json.JSONObject;

Expand Down Expand Up @@ -63,6 +70,7 @@ public class FirebaseAuthenticationPlugin extends Plugin {
"signInAnonymously cannot be used in combination with skipNativeAuth.";
public static final String AUTH_STATE_CHANGE_EVENT = "authStateChange";
public static final String ID_TOKEN_CHANGE_EVENT = "idTokenChange";

private FirebaseAuthenticationConfig config;
private FirebaseAuthentication implementation;

Expand Down Expand Up @@ -122,7 +130,7 @@ public void confirmVerificationCode(PluginCall call) {
return;
}
ConfirmVerificationCodeOptions options = new ConfirmVerificationCodeOptions(verificationId, verificationCode);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
NonEmptyErrorCallback callback = new NonEmptyErrorCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
Expand Down Expand Up @@ -181,7 +189,7 @@ public void fetchSignInMethodsForEmail(PluginCall call) {
}

FetchSignInMethodsForEmailOptions options = new FetchSignInMethodsForEmailOptions(email);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
NonEmptyErrorCallback callback = new NonEmptyErrorCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
Expand Down Expand Up @@ -223,7 +231,7 @@ public void getIdToken(PluginCall call) {
try {
Boolean forceRefresh = call.getBoolean("forceRefresh", false);

NonEmptyResultCallback callback = new NonEmptyResultCallback() {
NonEmptyErrorCallback callback = new NonEmptyErrorCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
Expand Down Expand Up @@ -468,7 +476,7 @@ public void revokeAccessToken(PluginCall call) {
}

RevokeAccessTokenOptions options = new RevokeAccessTokenOptions(token);
EmptyResultCallback callback = new EmptyResultCallback() {
EmptyErrorCallback callback = new EmptyErrorCallback() {
@Override
public void success() {
call.resolve();
Expand Down Expand Up @@ -496,7 +504,7 @@ public void sendEmailVerification(PluginCall call) {
JSObject actionCodeSettings = call.getObject("actionCodeSettings");

SendEmailVerificationOptions options = new SendEmailVerificationOptions(actionCodeSettings);
EmptyResultCallback callback = new EmptyResultCallback() {
EmptyErrorCallback callback = new EmptyErrorCallback() {
@Override
public void success() {
call.resolve();
Expand Down Expand Up @@ -529,7 +537,7 @@ public void sendPasswordResetEmail(PluginCall call) {
JSObject actionCodeSettings = call.getObject("actionCodeSettings");

SendPasswordResetEmailOptions options = new SendPasswordResetEmailOptions(email, actionCodeSettings);
EmptyResultCallback callback = new EmptyResultCallback() {
EmptyErrorCallback callback = new EmptyErrorCallback() {
@Override
public void success() {
call.resolve();
Expand Down Expand Up @@ -863,7 +871,7 @@ public void verifyBeforeUpdateEmail(PluginCall call) {
}
ActionCodeSettings actionCodeSettings = FirebaseAuthenticationHelper.createActionCodeSettings(settings);

EmptyResultCallback callback = new EmptyResultCallback() {
EmptyErrorCallback callback = new EmptyErrorCallback() {
@Override
public void success() {
call.resolve();
Expand Down Expand Up @@ -986,7 +994,7 @@ public void handleAuthStateChange() {
}

public void handleIdTokenChange() {
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
NonEmptyErrorCallback callback = new NonEmptyErrorCallback() {
@Override
public void success(Result result) {
notifyListeners(ID_TOKEN_CHANGE_EVENT, result.toJSObject(), true);
Expand All @@ -1000,15 +1008,6 @@ public void error(Exception exception) {
implementation.getIdToken(false, callback);
}

@Override
protected void handleOnActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.handleOnActivityResult(requestCode, resultCode, data);
if (data == null) {
return;
}
implementation.handleOnActivityResult(requestCode, resultCode, data);
}

@ActivityCallback
private void handlePlayGamesAuthProviderSignInActivityResult(@Nullable PluginCall call, @Nullable ActivityResult result) {
if (call == null || result == null) {
Expand Down
Loading

0 comments on commit 55941df

Please sign in to comment.