Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authentication): provide access token for Google on Android #98

Merged
merged 6 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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