From b7e1e6eb74986577082f574e1bcc05500283f68f Mon Sep 17 00:00:00 2001 From: Nisala Kalupahana Date: Sat, 21 May 2022 12:44:59 -0700 Subject: [PATCH] feat(authentication): provide access token for Google on Android (#98) Co-authored-by: Robin Genz --- .changeset/light-eyes-wash.md | 5 ++++ .../handlers/GoogleAuthProviderHandler.java | 24 ++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 .changeset/light-eyes-wash.md diff --git a/.changeset/light-eyes-wash.md b/.changeset/light-eyes-wash.md new file mode 100644 index 00000000..d10a3274 --- /dev/null +++ b/.changeset/light-eyes-wash.md @@ -0,0 +1,5 @@ +--- +"@capacitor-firebase/authentication": patch +--- + +feat(android): provide access token for Google diff --git a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java index b9b96ae9..aa78a92c 100644 --- a/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java +++ b/packages/authentication/android/src/main/java/dev/robingenz/capacitorjs/plugins/firebase/authentication/handlers/GoogleAuthProviderHandler.java @@ -5,8 +5,9 @@ import androidx.activity.result.ActivityResult; import androidx.annotation.Nullable; import com.getcapacitor.JSArray; -import com.getcapacitor.JSObject; import com.getcapacitor.PluginCall; +import com.google.android.gms.auth.GoogleAuthException; +import com.google.android.gms.auth.GoogleAuthUtil; import com.google.android.gms.auth.api.signin.GoogleSignIn; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInClient; @@ -16,10 +17,10 @@ import com.google.android.gms.tasks.Task; import com.google.firebase.auth.AuthCredential; import com.google.firebase.auth.GoogleAuthProvider; -import com.google.firebase.auth.OAuthCredential; import dev.robingenz.capacitorjs.plugins.firebase.authentication.FirebaseAuthentication; import dev.robingenz.capacitorjs.plugins.firebase.authentication.FirebaseAuthenticationPlugin; import dev.robingenz.capacitorjs.plugins.firebase.authentication.R; +import java.io.IOException; import java.util.List; import org.json.JSONException; @@ -50,7 +51,24 @@ public void handleOnActivityResult(PluginCall call, ActivityResult result) { GoogleSignInAccount account = task.getResult(ApiException.class); String idToken = account.getIdToken(); AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null); - pluginImplementation.handleSuccessfulSignIn(call, credential, idToken); + // Get Access Token and resolve + new Thread( + () -> { + String accessToken = null; + try { + accessToken = + GoogleAuthUtil.getToken(mGoogleSignInClient.getApplicationContext(), account.getAccount(), "oauth2:email"); + // Clears local cache after every login attempt + // to ensure permissions changes elsewhere are reflected in future tokens + GoogleAuthUtil.clearToken(mGoogleSignInClient.getApplicationContext(), accessToken); + } catch (IOException | GoogleAuthException exception) { + pluginImplementation.handleFailedSignIn(call, null, exception); + } + + pluginImplementation.handleSuccessfulSignIn(call, credential, idToken, null, accessToken); + } + ) + .start(); } catch (ApiException exception) { pluginImplementation.handleFailedSignIn(call, null, exception); }