CapacitorGoogleAuth
CapacitorGoogleAuth copied to clipboard
Check for an existing user?
As described here: https://developers.google.com/identity/sign-in/android/sign-in#check_for_an_existing_signed-in_user
I'm using this and that works great, I would love if this could make its way to a future release in some shape or form. See if you want to add modularity:
GoogleAuth.java:
@PluginMethod()
public void signIn(PluginCall call) {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this.getContext());
if (null == account) {
saveCall(call);
Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(call, signInIntent, RC_SIGN_IN);
} else {
JSObject user = this.accountToJSObject(account);
call.success(user);
}
}
...
public JSObject accountToJSObject(GoogleSignInAccount account) {
JSObject authentication = new JSObject();
authentication.put("idToken", account.getIdToken());
JSObject user = new JSObject();
user.put("serverAuthCode", account.getServerAuthCode());
user.put("idToken", account.getIdToken());
user.put("authentication", authentication);
user.put("displayName", account.getDisplayName());
user.put("email", account.getEmail());
user.put("familyName", account.getFamilyName());
user.put("givenName", account.getGivenName());
user.put("id", account.getId());
user.put("imageUrl", account.getPhotoUrl());
return user;
}
...
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
PluginCall signInCall = getSavedCall();
if (signInCall == null) return;
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
JSObject user = this.accountToJSObject(account);
signInCall.success(user);
}
catch (ApiException e) {
signInCall.error("Something went wrong", e);
}
}
Sorry, I'm getting tired, and I wish to go to sleep more than open a proper pull request. This implementation avoids having the native popup show and immediately disappear when asking for a new IdToken.
Would be closed with https://github.com/CodetrixStudio/CapacitorGoogleAuth/pull/78/
Closing, stale