Skip to content

Commit

Permalink
feat(authentication): provide access token for Google on Android (#98)
Browse files Browse the repository at this point in the history
Co-authored-by: Robin Genz <mail@robingenz.dev>
  • Loading branch information
nkalupahana and robingenz authored May 21, 2022
1 parent a51927f commit b7e1e6e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-eyes-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@capacitor-firebase/authentication": patch
---

feat(android): provide access token for Google
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit b7e1e6e

Please sign in to comment.