@capgo/capacitor-social-login 8.7.2 → 8.7.4

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.
@@ -61,6 +61,8 @@ public class AppleProvider implements SocialProvider {
61
61
  private static final String APPLE_DATA_PREFERENCE = "APPLE_LOGIN_APPLE_DATA_83b2d6db-17fe-49c9-8c33-e3f5d02f9f84";
62
62
 
63
63
  private PluginCall lastcall;
64
+ private String pendingRedirectState;
65
+ private final AppleRedirectLoginCoordinator redirectLoginCoordinator = new AppleRedirectLoginCoordinator();
64
66
  private String appleAuthURLFull;
65
67
 
66
68
  private String idToken;
@@ -125,6 +127,19 @@ public class AppleProvider implements SocialProvider {
125
127
  }
126
128
  }
127
129
 
130
+ /**
131
+ * Called when the host activity resumes after Custom Tabs without receiving a redirect.
132
+ */
133
+ void handleUserReturnedWithoutCallback() {
134
+ if (!redirectLoginCoordinator.shouldRejectOnActivityResume()) {
135
+ return;
136
+ }
137
+ if (lastcall != null) {
138
+ lastcall.reject(AppleRedirectLoginCoordinator.USER_CANCELLED_MESSAGE, AppleRedirectLoginCoordinator.USER_CANCELLED_CODE);
139
+ }
140
+ clearPendingLoginCall();
141
+ }
142
+
128
143
  public void handleIntent(Intent intent) {
129
144
  // Extract information from the URI
130
145
  Uri data = intent.getData();
@@ -222,6 +237,7 @@ public class AppleProvider implements SocialProvider {
222
237
 
223
238
  private void loginWithRedirect(PluginCall call, JSONObject config) {
224
239
  String state = UUID.randomUUID().toString();
240
+ this.pendingRedirectState = state;
225
241
 
226
242
  // Extract scopes from config
227
243
  String scopes = DEFAULT_SCOPE;
@@ -262,7 +278,7 @@ public class AppleProvider implements SocialProvider {
262
278
 
263
279
  if (context == null || activity == null) {
264
280
  this.lastcall.reject("Context or Activity is null");
265
- this.lastcall = null;
281
+ clearPendingLoginCall();
266
282
  return;
267
283
  }
268
284
 
@@ -320,6 +336,13 @@ public class AppleProvider implements SocialProvider {
320
336
  }
321
337
 
322
338
  Uri uri = Uri.parse(url);
339
+ String callbackState = uri.getQueryParameter("state");
340
+ if (!matchesPendingRedirectState(pendingRedirectState, callbackState)) {
341
+ return;
342
+ }
343
+
344
+ redirectLoginCoordinator.onRedirectReceived();
345
+
323
346
  String success = uri.getQueryParameter("success");
324
347
  if ("true".equals(success)) {
325
348
  String accessToken = uri.getQueryParameter("access_token");
@@ -359,6 +382,7 @@ public class AppleProvider implements SocialProvider {
359
382
  this.lastcall.reject("Expected proper tokens from backend but received authorization code only");
360
383
  } else {
361
384
  // Legacy mode: exchange the authorization code for tokens
385
+ redirectLoginCoordinator.onTokenExchangeStarted();
362
386
  requestForAccessToken(appleAuthCode, appleClientSecret);
363
387
  return; // Don't clear lastcall here, it will be cleared in the callback
364
388
  }
@@ -366,7 +390,17 @@ public class AppleProvider implements SocialProvider {
366
390
  } else {
367
391
  this.lastcall.reject("We couldn't get the Auth Code");
368
392
  }
393
+ clearPendingLoginCall();
394
+ }
395
+
396
+ private void clearPendingLoginCall() {
369
397
  this.lastcall = null;
398
+ this.pendingRedirectState = null;
399
+ redirectLoginCoordinator.onLoginFinished();
400
+ }
401
+
402
+ static boolean matchesPendingRedirectState(String pendingState, String callbackState) {
403
+ return pendingState != null && pendingState.equals(callbackState);
370
404
  }
371
405
 
372
406
  private void requestForAccessToken(String code, String clientSecret) {
@@ -389,7 +423,7 @@ public class AppleProvider implements SocialProvider {
389
423
  public void onFailure(@NonNull Call call, @NonNull IOException e) {
390
424
  if (AppleProvider.this.lastcall != null) {
391
425
  AppleProvider.this.lastcall.reject("Cannot get access_token", e);
392
- AppleProvider.this.lastcall = null;
426
+ clearPendingLoginCall();
393
427
  } else {
394
428
  Log.e(SocialLoginPlugin.LOG_TAG, "Cannot get access_token: lastcall is null. Error: " + e.getMessage(), e);
395
429
  }
@@ -423,14 +457,14 @@ public class AppleProvider implements SocialProvider {
423
457
 
424
458
  if (AppleProvider.this.lastcall != null) {
425
459
  AppleProvider.this.lastcall.resolve(appleResponse);
426
- AppleProvider.this.lastcall = null;
460
+ clearPendingLoginCall();
427
461
  } else {
428
462
  Log.e(SocialLoginPlugin.LOG_TAG, "Cannot resolve access_token response: lastcall is null");
429
463
  }
430
464
  } catch (Exception e) {
431
465
  if (AppleProvider.this.lastcall != null) {
432
466
  AppleProvider.this.lastcall.reject("Cannot get access_token", e);
433
- AppleProvider.this.lastcall = null;
467
+ clearPendingLoginCall();
434
468
  } else {
435
469
  Log.e(SocialLoginPlugin.LOG_TAG, "Cannot get access_token: lastcall is null. Error: " + e.getMessage(), e);
436
470
  }
@@ -472,6 +506,7 @@ public class AppleProvider implements SocialProvider {
472
506
 
473
507
  @SuppressLint("SetJavaScriptEnabled")
474
508
  private void setupWebview(Context context, Activity activity, PluginCall call, String url) {
509
+ redirectLoginCoordinator.onCustomTabLaunched();
475
510
  CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(getCustomTabsSession());
476
511
 
477
512
  builder.build().launchUrl(context, Uri.parse(url));
@@ -0,0 +1,36 @@
1
+ package ee.forgr.capacitor.social.login;
2
+
3
+ /**
4
+ * Tracks Custom Tabs redirect login state so we can detect when the user returns without a callback.
5
+ */
6
+ final class AppleRedirectLoginCoordinator {
7
+
8
+ static final String USER_CANCELLED_CODE = "USER_CANCELLED";
9
+ static final String USER_CANCELLED_MESSAGE = "User cancelled";
10
+
11
+ private boolean customTabsRedirectPending;
12
+ private boolean tokenExchangeInProgress;
13
+
14
+ void onCustomTabLaunched() {
15
+ customTabsRedirectPending = true;
16
+ tokenExchangeInProgress = false;
17
+ }
18
+
19
+ void onRedirectReceived() {
20
+ customTabsRedirectPending = false;
21
+ }
22
+
23
+ void onTokenExchangeStarted() {
24
+ customTabsRedirectPending = false;
25
+ tokenExchangeInProgress = true;
26
+ }
27
+
28
+ void onLoginFinished() {
29
+ customTabsRedirectPending = false;
30
+ tokenExchangeInProgress = false;
31
+ }
32
+
33
+ boolean shouldRejectOnActivityResume() {
34
+ return customTabsRedirectPending && !tokenExchangeInProgress;
35
+ }
36
+ }
@@ -27,7 +27,7 @@ import org.json.JSONObject;
27
27
  @CapacitorPlugin(name = "SocialLogin")
28
28
  public class SocialLoginPlugin extends Plugin {
29
29
 
30
- private final String pluginVersion = "8.7.2";
30
+ private final String pluginVersion = "8.7.4";
31
31
 
32
32
  public static String LOG_TAG = "CapgoSocialLogin";
33
33
  private static final String USER_CANCELLED_CODE = "USER_CANCELLED";
@@ -887,6 +887,11 @@ public class SocialLoginPlugin extends Plugin {
887
887
  ((OAuth2Provider) oauth2Provider).handleUserReturnedWithoutCallback();
888
888
  }
889
889
 
890
+ SocialProvider appleProvider = socialProviderHashMap.get("apple");
891
+ if (appleProvider instanceof AppleProvider) {
892
+ ((AppleProvider) appleProvider).handleUserReturnedWithoutCallback();
893
+ }
894
+
890
895
  // User returned without redirect: cancel when we saw pause, or after launch grace if pause was missed
891
896
  if (openAuthSessionSavedCall != null) {
892
897
  boolean pausedAfterLaunch = openAuthSessionPaused;
@@ -16,7 +16,7 @@ import GoogleSignIn
16
16
  */
17
17
  @objc(SocialLoginPlugin)
18
18
  public class SocialLoginPlugin: CAPPlugin, CAPBridgedPlugin {
19
- private let pluginVersion: String = "8.7.2"
19
+ private let pluginVersion: String = "8.7.4"
20
20
  public let identifier = "SocialLoginPlugin"
21
21
  public let jsName = "SocialLogin"
22
22
  private static let userCancelledCode = "USER_CANCELLED"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-social-login",
3
- "version": "8.7.2",
3
+ "version": "8.7.4",
4
4
  "description": "All social logins in one plugin",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",