@capgo/capacitor-social-login 8.3.18 → 8.3.19

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
@@ -455,37 +455,69 @@ var capacitorCapacitorUpdater = (function (exports, core) {
455
455
  throw new Error('Facebook App ID not set. Call initialize() first.');
456
456
  }
457
457
  return new Promise((resolve, reject) => {
458
+ var _a;
459
+ let settled = false;
460
+ let waitingForStatus = false;
461
+ const resolveWithProfile = (authResponse) => {
462
+ if (settled)
463
+ return;
464
+ if (!(authResponse === null || authResponse === void 0 ? void 0 : authResponse.accessToken) || !authResponse.userID) {
465
+ settled = true;
466
+ reject(new Error('Facebook login failed'));
467
+ return;
468
+ }
469
+ const accessToken = authResponse.accessToken;
470
+ const userId = authResponse.userID;
471
+ FB.api('/me', { fields: 'id,name,email,picture' }, (userInfo) => {
472
+ var _a, _b;
473
+ settled = true;
474
+ const result = {
475
+ accessToken: {
476
+ token: accessToken,
477
+ userId,
478
+ },
479
+ profile: {
480
+ userID: userInfo.id,
481
+ name: userInfo.name,
482
+ email: userInfo.email || null,
483
+ imageURL: ((_b = (_a = userInfo.picture) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.url) || null,
484
+ friendIDs: [],
485
+ birthday: null,
486
+ ageRange: null,
487
+ gender: null,
488
+ location: null,
489
+ hometown: null,
490
+ profileURL: null,
491
+ },
492
+ idToken: null,
493
+ };
494
+ resolve({ provider: 'facebook', result });
495
+ });
496
+ };
497
+ const waitForConnected = () => {
498
+ if (settled || waitingForStatus)
499
+ return;
500
+ waitingForStatus = true;
501
+ this.waitForConnection()
502
+ .then((statusResponse) => resolveWithProfile(statusResponse.authResponse))
503
+ .catch((error) => {
504
+ if (settled)
505
+ return;
506
+ settled = true;
507
+ reject(error);
508
+ });
509
+ };
458
510
  FB.login((response) => {
459
511
  if (response.status === 'connected') {
460
- FB.api('/me', { fields: 'id,name,email,picture' }, (userInfo) => {
461
- var _a, _b;
462
- const result = {
463
- accessToken: {
464
- token: response.authResponse.accessToken,
465
- userId: response.authResponse.userID,
466
- },
467
- profile: {
468
- userID: userInfo.id,
469
- name: userInfo.name,
470
- email: userInfo.email || null,
471
- imageURL: ((_b = (_a = userInfo.picture) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.url) || null,
472
- friendIDs: [],
473
- birthday: null,
474
- ageRange: null,
475
- gender: null,
476
- location: null,
477
- hometown: null,
478
- profileURL: null,
479
- },
480
- idToken: null,
481
- };
482
- resolve({ provider: 'facebook', result });
483
- });
512
+ resolveWithProfile(response.authResponse);
513
+ }
514
+ else if (response.status === 'not_authorized' || response.status === 'unknown') {
515
+ reject(new Error('Facebook login was cancelled.'));
484
516
  }
485
517
  else {
486
- reject(new Error('Facebook login failed'));
518
+ waitForConnected();
487
519
  }
488
- }, { scope: options.permissions.join(',') });
520
+ }, ((_a = options.permissions) === null || _a === void 0 ? void 0 : _a.length) ? { scope: options.permissions.join(',') } : undefined);
489
521
  });
490
522
  }
491
523
  async logout() {
@@ -516,6 +548,38 @@ var capacitorCapacitorUpdater = (function (exports, core) {
516
548
  async refresh(options) {
517
549
  await this.login(options);
518
550
  }
551
+ waitForConnection(timeoutMs = 120000, pollIntervalMs = 500) {
552
+ const start = Date.now();
553
+ return new Promise((resolve, reject) => {
554
+ let finished = false;
555
+ const pollStatus = () => {
556
+ if (finished)
557
+ return;
558
+ FB.getLoginStatus((response) => {
559
+ var _a;
560
+ if (finished)
561
+ return;
562
+ if (response.status === 'connected' && ((_a = response.authResponse) === null || _a === void 0 ? void 0 : _a.accessToken)) {
563
+ finished = true;
564
+ resolve(response);
565
+ return;
566
+ }
567
+ if (response.status === 'not_authorized' || response.status === 'unknown') {
568
+ finished = true;
569
+ reject(new Error('Facebook login was cancelled.'));
570
+ return;
571
+ }
572
+ if (Date.now() - start >= timeoutMs) {
573
+ finished = true;
574
+ reject(new Error('Facebook login failed or timed out'));
575
+ return;
576
+ }
577
+ setTimeout(pollStatus, pollIntervalMs);
578
+ });
579
+ };
580
+ pollStatus();
581
+ });
582
+ }
519
583
  async loadFacebookScript(locale) {
520
584
  if (this.scriptLoaded)
521
585
  return;
@@ -978,10 +1042,13 @@ var capacitorCapacitorUpdater = (function (exports, core) {
978
1042
  */
979
1043
  class OAuth2SocialLogin extends BaseSocialLogin {
980
1044
  constructor() {
981
- super(...arguments);
1045
+ super();
982
1046
  this.providers = new Map();
983
1047
  this.TOKENS_KEY_PREFIX = 'capgo_social_login_oauth2_tokens_';
984
1048
  this.STATE_PREFIX = 'capgo_social_login_oauth2_state_';
1049
+ this.CONFIG_KEY_PREFIX = 'capgo_social_login_oauth2_config_';
1050
+ // Restore configurations from localStorage for popup window context
1051
+ this.restoreConfigurationsFromStorage();
985
1052
  }
986
1053
  normalizeScopeValue(scope) {
987
1054
  if (!scope)
@@ -1064,6 +1131,8 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1064
1131
  logoutUrl: config.logoutUrl,
1065
1132
  });
1066
1133
  }
1134
+ // Persist updated configuration after discovery
1135
+ this.persistConfiguration(providerId, config);
1067
1136
  }
1068
1137
  /**
1069
1138
  * Initialize multiple OAuth2 providers
@@ -1084,6 +1153,8 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1084
1153
  }
1085
1154
  // Pre-resolve discovery on web if issuerUrl is provided.
1086
1155
  await this.ensureDiscovered(providerId);
1156
+ // Persist configuration to localStorage so it's available in popup window context
1157
+ this.persistConfiguration(providerId, internalConfig);
1087
1158
  }
1088
1159
  }
1089
1160
  getProvider(providerId) {
@@ -1096,6 +1167,37 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1096
1167
  getTokensKey(providerId) {
1097
1168
  return `${this.TOKENS_KEY_PREFIX}${providerId}`;
1098
1169
  }
1170
+ getConfigKey(providerId) {
1171
+ return `${this.CONFIG_KEY_PREFIX}${providerId}`;
1172
+ }
1173
+ persistConfiguration(providerId, config) {
1174
+ try {
1175
+ localStorage.setItem(this.getConfigKey(providerId), JSON.stringify(config));
1176
+ }
1177
+ catch (err) {
1178
+ console.warn(`Failed to persist OAuth2 configuration for provider '${providerId}'`, err);
1179
+ }
1180
+ }
1181
+ restoreConfigurationsFromStorage() {
1182
+ // Restore all provider configurations from localStorage
1183
+ // This is needed for popup window context where the parent's in-memory state is not available
1184
+ const keys = Object.keys(localStorage);
1185
+ for (const key of keys) {
1186
+ if (key.startsWith(this.CONFIG_KEY_PREFIX)) {
1187
+ const providerId = key.substring(this.CONFIG_KEY_PREFIX.length);
1188
+ try {
1189
+ const raw = localStorage.getItem(key);
1190
+ if (raw) {
1191
+ const config = JSON.parse(raw);
1192
+ this.providers.set(providerId, config);
1193
+ }
1194
+ }
1195
+ catch (err) {
1196
+ console.warn(`Failed to restore OAuth2 configuration for provider '${providerId}'`, err);
1197
+ }
1198
+ }
1199
+ }
1200
+ }
1099
1201
  async login(options) {
1100
1202
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1101
1203
  const { providerId } = options;
@@ -1671,6 +1773,7 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1671
1773
  const codeVerifier = (_e = options.codeVerifier) !== null && _e !== void 0 ? _e : this.generateCodeVerifier();
1672
1774
  const codeChallenge = await this.generateCodeChallenge(codeVerifier);
1673
1775
  this.persistPendingLogin(state, {
1776
+ clientId: this.clientId,
1674
1777
  codeVerifier,
1675
1778
  redirectUri,
1676
1779
  scopes,
@@ -1876,10 +1979,9 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1876
1979
  }
1877
1980
  }
1878
1981
  async exchangeAuthorizationCode(code, pending) {
1879
- var _a;
1880
1982
  const params = new URLSearchParams({
1881
1983
  grant_type: 'authorization_code',
1882
- client_id: (_a = this.clientId) !== null && _a !== void 0 ? _a : '',
1984
+ client_id: pending.clientId,
1883
1985
  code,
1884
1986
  redirect_uri: pending.redirectUri,
1885
1987
  code_verifier: pending.codeVerifier,