capacitor-native-google-one-tap-signin 0.0.13 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  package com.proventask.capacitor.GoogleAuth;
2
2
 
3
3
  import androidx.activity.result.ActivityResultCallback;
4
+ import androidx.activity.result.ActivityResultLauncher;
5
+ import androidx.activity.result.IntentSenderRequest;
4
6
  import androidx.activity.result.contract.ActivityResultContracts;
5
7
  import androidx.annotation.NonNull;
6
8
  import androidx.annotation.Nullable;
@@ -30,6 +32,7 @@ import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
30
32
  import com.google.android.gms.auth.api.signin.GoogleSignInClient;
31
33
  import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
32
34
  import com.google.android.gms.common.api.ApiException;
35
+ import com.google.android.gms.common.api.ResolvableApiException;
33
36
  import com.google.android.gms.common.api.Scope;
34
37
  import com.google.android.gms.tasks.Task;
35
38
 
@@ -73,6 +76,8 @@ import java.util.concurrent.Executors;
73
76
 
74
77
  @CapacitorPlugin()
75
78
  public class GoogleAuth extends Plugin {
79
+
80
+ private static final String TAG = "GoogleAuth Plugin";
76
81
  private final static String VERIFY_TOKEN_URL = "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=";
77
82
  private final static String FIELD_TOKEN_EXPIRES_IN = "expires_in";
78
83
  private final static String FIELD_ACCESS_TOKEN = "accessToken";
@@ -84,14 +89,22 @@ public class GoogleAuth extends Plugin {
84
89
  public static final int KAssumeStaleTokenSec = 60;
85
90
 
86
91
  private static final int REQ_ONE_TAP = 2;
92
+ private static final int RC_CREDENTIALS_READ = 3;
87
93
 
88
94
  private String webClientId;
89
95
  private SignInClient oneTapClient;
90
96
  private BeginSignInRequest signInRequest;
91
97
  private CredentialsClient mCredentialsClient;
92
98
 
99
+ private GoogleSignInClient mSignInClient;
100
+
93
101
  private String callbackId;
94
102
 
103
+ private boolean mIsResolving = false;
104
+ private ActivityResultLauncher<IntentSenderRequest> googleOneTapSignInActivityResultHandler;
105
+ //private ActivityResultLauncher<IntentSender> googleOneTapSignInActivityResultHandler;
106
+ //private ActivityResultLauncher<Intent> googleOneTapSignInActivityResultHandler;
107
+
95
108
  @Override
96
109
  public void load() {
97
110
  String clientId = getConfig()
@@ -122,31 +135,67 @@ public class GoogleAuth extends Plugin {
122
135
  // GoogleSignInOptions googleSignInOptions = googleSignInBuilder.build();
123
136
  // googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions);
124
137
 
125
- oneTapClient = Identity.getSignInClient(this.getContext());
138
+ oneTapClient = Identity.getSignInClient(this.getActivity());
126
139
 
127
140
  mCredentialsClient = Credentials.getClient(this.getContext());
128
141
 
142
+ GoogleSignInOptions.Builder gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
143
+ .requestIdToken(clientId);
144
+
145
+ mSignInClient = GoogleSignIn.getClient(this.getContext(), gsoBuilder.build());
146
+
129
147
  // todo
130
148
  // reactContext.addActivityEventListener(new RNGoogleOneTapSignInActivityEventListener());
131
149
 
132
- // var googleOneTapSignInActivityResultHandler = bridge.registerForActivityResult(
133
- // new ActivityResultContracts.StartIntentSenderForResult(),
134
- // (ActivityResultCallback<ActivityResult>) result -> {
135
- // PluginCall savedCall = this.bridge.getSavedCall(callbackId);
136
- // var intent = result.getData();
137
- // var typeName = result.getClass();
150
+ googleOneTapSignInActivityResultHandler = bridge.registerForActivityResult(
151
+ new ActivityResultContracts.StartIntentSenderForResult(), // new ActivityResultContracts.StartActivityForResult(),
152
+ (ActivityResultCallback<ActivityResult>) result -> {
153
+
154
+ var code = result.getResultCode();
155
+ Intent data = result.getData();
156
+
157
+ if (code == Activity.RESULT_OK) {
158
+ //handleSignInResult(result);
159
+
160
+ try {
161
+ SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(data);
162
+ } catch (ApiException e) {
163
+ throw new RuntimeException(e);
164
+ }
165
+ }
166
+ var isCancel = code == Activity.RESULT_CANCELED;
167
+
168
+ // handleSignInResult(result);
169
+
170
+ // switch (result.getResultCode()) {
171
+ // case Activity.RESULT_OK:
172
+ // Intent data = result.getData();
173
+ // handleSignInResult(result);
138
174
  //
139
- // switch (result.getResultCode()) {
140
- // case Activity.RESULT_OK:
141
- // Intent data = result.getData();
142
- // //PaymentData paymentData = PaymentData.getFromIntent(data);
143
- // // ...
144
- // break;
145
- // case Activity.RESULT_CANCELED:
146
- // // ...
147
- // break;
148
- // }
149
- // });
175
+ // PluginCall savedCall = this.bridge.getSavedCall(callbackId);
176
+ // var intent = result.getData();
177
+ //
178
+ //
179
+ // //PaymentData paymentData = PaymentData.getFromIntent(data);
180
+ // // ...
181
+ // break;
182
+ // case Activity.RESULT_CANCELED:
183
+ // // ...
184
+ // break;
185
+ //}
186
+ });
187
+
188
+ }
189
+
190
+ private void handleSignInResult(ActivityResult result) {
191
+ Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
192
+ try {
193
+ var account = task.getResult();
194
+ var email = account.getEmail();
195
+ Log.d(TAG, "handleSignInResult() called with: result = $account");
196
+ } catch (Exception ex) {
197
+ Log.e(TAG, "handleSignInResult: ", ex);
198
+ }
150
199
  }
151
200
 
152
201
  // private class RNGoogleOneTapSignInActivityEventListener extends BaseActivityEventListener {
@@ -196,6 +245,68 @@ public class GoogleAuth extends Plugin {
196
245
 
197
246
  @PluginMethod()
198
247
  public void signIn(PluginCall call) {
248
+ CredentialRequest.Builder crBuilder = new CredentialRequest.Builder();
249
+ crBuilder.setAccountTypes(IdentityProviders.GOOGLE);
250
+
251
+ this.callbackId = call.getCallbackId();
252
+ this.bridge.saveCall(call);
253
+
254
+ mCredentialsClient.request(crBuilder.build()).addOnCompleteListener(
255
+ new OnCompleteListener<CredentialRequestResponse>() {
256
+ @Override
257
+ public void onComplete(@NonNull Task<CredentialRequestResponse> task) {
258
+
259
+ if (task.isSuccessful()) {
260
+ // Auto sign-in success
261
+ //handleCredential(task.getResult().getCredential());
262
+ return;
263
+ }
264
+
265
+ Exception e = task.getException();
266
+ if (e instanceof ResolvableApiException) {
267
+ // Getting credential needs to show some UI, start resolution
268
+ ResolvableApiException rae = (ResolvableApiException) e;
269
+ resolveResult(rae, RC_CREDENTIALS_READ);
270
+ } else {
271
+ Log.w(TAG, "request: not handling exception", e);
272
+ }
273
+ }
274
+ });
275
+ }
276
+
277
+ private void resolveResult(ResolvableApiException rae, int requestCode) {
278
+ if (!mIsResolving) {
279
+ //try {
280
+ // var resolutionIntent = rae.getResolution();
281
+ var intentSender = rae.getResolution().getIntentSender();
282
+
283
+
284
+ var intentSenderRequest = new IntentSenderRequest.Builder(intentSender).build();
285
+ googleOneTapSignInActivityResultHandler.launch(intentSenderRequest);
286
+
287
+
288
+ //rae.startResolutionForResult(this.getActivity(), requestCode);
289
+ //googleOneTapSignInActivityResultHandler.launch(mSignInClient.getSignInIntent());
290
+
291
+ // var x = bridge.registerForActivityResult(
292
+ // new ActivityResultContracts.StartIntentSenderForResult(),
293
+ // (ActivityResultCallback<ActivityResult>) result -> {
294
+ //
295
+ // Log.d(TAG, "signIn() called ${it.data}");
296
+ // Log.d(TAG, "signIn() called ${it.resultCode}");
297
+ //
298
+ // });
299
+
300
+ mIsResolving = true;
301
+ //} catch (IntentSender.SendIntentException e) {
302
+ // Log.e(TAG, "Failed to send Credentials intent.", e);
303
+ // mIsResolving = false;
304
+ //}
305
+ }
306
+ }
307
+
308
+ @PluginMethod()
309
+ public void signIn2(PluginCall call) {
199
310
  if (oneTapClient == null) {
200
311
  call.reject("oneTapClient is null, call initialize first."); // todo: is initialize correct?
201
312
  return;
@@ -230,6 +341,8 @@ public class GoogleAuth extends Plugin {
230
341
  // runOnUiThread(new Runnable() {
231
342
  // @Override
232
343
  // public void run() {
344
+
345
+
233
346
  oneTapClient.beginSignIn(signInRequest)
234
347
  .addOnSuccessListener(new MyEventListener(call))
235
348
  .addOnFailureListener(new MyEventListener(call));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-native-google-one-tap-signin",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Google One Tap sign-in plugin for capacitor.",
5
5
  "main": "dist/esm/index.js",
6
6
  "types": "dist/esm/index.d.ts",