@capgo/capacitor-social-login 8.5.19 → 8.6.0

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.
package/dist/plugin.js CHANGED
@@ -2432,6 +2432,9 @@ var capacitorCapacitorUpdater = (function (exports, core) {
2432
2432
  async openSecureWindow(options) {
2433
2433
  return rawSocialLogin.openSecureWindow(options);
2434
2434
  }
2435
+ async openAuthSession(options) {
2436
+ return rawSocialLogin.openAuthSession(options);
2437
+ }
2435
2438
  }
2436
2439
  const SocialLoginBase = rawSocialLogin;
2437
2440
  const SocialLogin = new SocialLoginClient();
@@ -2934,9 +2937,25 @@ var capacitorCapacitorUpdater = (function (exports, core) {
2934
2937
  }
2935
2938
  }
2936
2939
 
2940
+ /** Grace period before treating popup.closed as user cancellation (COOP can sever opener early). */
2941
+ const POPUP_CLOSED_CANCEL_GRACE_MS = 2500;
2942
+ function nextPopupClosePollState(closed, state, nowMs, graceMs = POPUP_CLOSED_CANCEL_GRACE_MS) {
2943
+ if (!closed) {
2944
+ return { state: { graceStartedAtMs: null }, shouldCancel: false };
2945
+ }
2946
+ if (state.graceStartedAtMs == null) {
2947
+ return { state: { graceStartedAtMs: nowMs }, shouldCancel: false };
2948
+ }
2949
+ if (nowMs - state.graceStartedAtMs >= graceMs) {
2950
+ return { state, shouldCancel: true };
2951
+ }
2952
+ return { state, shouldCancel: false };
2953
+ }
2954
+
2937
2955
  class SocialLoginWeb extends core.WebPlugin {
2938
2956
  constructor() {
2939
2957
  super();
2958
+ this.webAuthPopupInProgress = false;
2940
2959
  this.googleProvider = new GoogleSocialLogin();
2941
2960
  this.appleProvider = new AppleSocialLogin();
2942
2961
  this.facebookProvider = new FacebookSocialLogin();
@@ -3273,35 +3292,147 @@ var capacitorCapacitorUpdater = (function (exports, core) {
3273
3292
  return { version: 'web' };
3274
3293
  }
3275
3294
  async openSecureWindow(options) {
3276
- const w = 600;
3277
- const h = 550;
3278
- const settings = [
3279
- ['width', w],
3280
- ['height', h],
3281
- ['left', screen.width / 2 - w / 2],
3282
- ['top', screen.height / 2 - h / 2],
3283
- ]
3284
- .map((x) => x.join('='))
3285
- .join(',');
3286
- const popup = window.open(options.authEndpoint, 'Authorization', settings);
3287
- if (typeof popup.focus === 'function') {
3288
- popup.focus();
3295
+ const callbackURL = await this.runWebAuthPopup({
3296
+ url: options.authEndpoint,
3297
+ redirectPrefix: options.redirectUri,
3298
+ broadcastChannelName: options.broadcastChannelName,
3299
+ });
3300
+ return { redirectedUri: callbackURL };
3301
+ }
3302
+ async openAuthSession(options) {
3303
+ var _a;
3304
+ const redirectPrefixes = [`${options.callbackURLScheme}://`];
3305
+ try {
3306
+ const authUrl = new URL(options.url);
3307
+ const redirectUri = authUrl.searchParams.get('redirect_uri');
3308
+ if (redirectUri && /^https?:\/\//i.test(redirectUri) && !redirectPrefixes.includes(redirectUri)) {
3309
+ redirectPrefixes.unshift(redirectUri);
3310
+ }
3289
3311
  }
3290
- return new Promise((resolve, reject) => {
3291
- const bc = new BroadcastChannel(options.broadcastChannelName || 'oauth-channel');
3292
- bc.addEventListener('message', (event) => {
3293
- if (event.data.startsWith(options.redirectUri)) {
3312
+ catch (_b) {
3313
+ // ignore malformed authorize URLs; scheme prefix remains the fallback
3314
+ }
3315
+ const callbackURL = await this.runWebAuthPopup({
3316
+ url: options.url,
3317
+ redirectPrefixes,
3318
+ broadcastChannelName: (_a = options.broadcastChannelName) !== null && _a !== void 0 ? _a : 'capgo-auth-session',
3319
+ });
3320
+ return { callbackURL };
3321
+ }
3322
+ runWebAuthPopup(options) {
3323
+ var _a;
3324
+ if (this.webAuthPopupInProgress) {
3325
+ return Promise.reject(new Error('Another auth session is already in progress'));
3326
+ }
3327
+ this.webAuthPopupInProgress = true;
3328
+ let popup = null;
3329
+ let bc = null;
3330
+ let pollTimer = 0;
3331
+ let timeoutTimer = 0;
3332
+ const releasePopupGuard = () => {
3333
+ this.webAuthPopupInProgress = false;
3334
+ if (pollTimer) {
3335
+ window.clearInterval(pollTimer);
3336
+ pollTimer = 0;
3337
+ }
3338
+ if (timeoutTimer) {
3339
+ window.clearTimeout(timeoutTimer);
3340
+ timeoutTimer = 0;
3341
+ }
3342
+ if (bc) {
3343
+ try {
3294
3344
  bc.close();
3295
- resolve({ redirectedUri: event.data });
3296
3345
  }
3297
- else {
3298
- bc.close();
3299
- reject(new Error('Redirect URI does not match, expected ' + options.redirectUri + ' but got ' + event.data));
3346
+ catch (_a) {
3347
+ // ignore close errors
3348
+ }
3349
+ bc = null;
3350
+ }
3351
+ };
3352
+ try {
3353
+ const w = 600;
3354
+ const h = 550;
3355
+ const settings = [
3356
+ ['width', w],
3357
+ ['height', h],
3358
+ ['left', screen.width / 2 - w / 2],
3359
+ ['top', screen.height / 2 - h / 2],
3360
+ ]
3361
+ .map((x) => x.join('='))
3362
+ .join(',');
3363
+ popup = window.open(options.url, 'Authorization', settings);
3364
+ if (!popup) {
3365
+ releasePopupGuard();
3366
+ return Promise.reject(new Error('Unable to open authorization popup. Allow popups for this origin.'));
3367
+ }
3368
+ if (typeof popup.focus === 'function') {
3369
+ popup.focus();
3370
+ }
3371
+ const channelName = (_a = options.broadcastChannelName) !== null && _a !== void 0 ? _a : 'oauth-channel';
3372
+ bc = new BroadcastChannel(channelName);
3373
+ }
3374
+ catch (error) {
3375
+ releasePopupGuard();
3376
+ if (popup && !popup.closed) {
3377
+ try {
3378
+ popup.close();
3379
+ }
3380
+ catch (_b) {
3381
+ // ignore cross-origin close errors
3382
+ }
3383
+ }
3384
+ throw error;
3385
+ }
3386
+ const activePopup = popup;
3387
+ const activeBc = bc;
3388
+ if (!activeBc) {
3389
+ releasePopupGuard();
3390
+ return Promise.reject(new Error('Unable to open authorization channel'));
3391
+ }
3392
+ return new Promise((resolve, reject) => {
3393
+ var _a;
3394
+ let settled = false;
3395
+ const prefixes = (_a = options.redirectPrefixes) !== null && _a !== void 0 ? _a : (options.redirectPrefix != null ? [options.redirectPrefix] : []);
3396
+ const finish = (handler) => {
3397
+ if (settled) {
3398
+ return;
3399
+ }
3400
+ settled = true;
3401
+ releasePopupGuard();
3402
+ handler();
3403
+ };
3404
+ activeBc.addEventListener('message', (event) => {
3405
+ var _a;
3406
+ const data = typeof event.data === 'string' ? event.data : String((_a = event.data) !== null && _a !== void 0 ? _a : '');
3407
+ if (prefixes.some((prefix) => data.startsWith(prefix))) {
3408
+ finish(() => resolve(data));
3409
+ return;
3410
+ }
3411
+ const looksLikeRedirect = /^https?:\/\//i.test(data) || /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(data);
3412
+ if (looksLikeRedirect) {
3413
+ finish(() => reject(new Error('Redirect URI does not match expected prefix. Received: ' +
3414
+ data +
3415
+ ' Expected one of: ' +
3416
+ prefixes.join(', '))));
3300
3417
  }
3301
3418
  });
3302
- setTimeout(() => {
3303
- bc.close();
3304
- reject(new Error('The sign-in flow timed out'));
3419
+ let popupClosePollState = { graceStartedAtMs: null };
3420
+ pollTimer = window.setInterval(() => {
3421
+ try {
3422
+ const closed = activePopup.closed;
3423
+ const pollResult = nextPopupClosePollState(closed, popupClosePollState, Date.now());
3424
+ popupClosePollState = pollResult.state;
3425
+ if (pollResult.shouldCancel) {
3426
+ finish(() => reject(createUserCancelledError('User cancelled authorization')));
3427
+ }
3428
+ }
3429
+ catch (_a) {
3430
+ window.clearInterval(pollTimer);
3431
+ pollTimer = 0;
3432
+ }
3433
+ }, 500);
3434
+ timeoutTimer = window.setTimeout(() => {
3435
+ finish(() => reject(new Error('The sign-in flow timed out')));
3305
3436
  }, 5 * 60000);
3306
3437
  });
3307
3438
  }