@capgo/capacitor-social-login 8.3.17 → 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;
@@ -854,13 +918,21 @@ var capacitorCapacitorUpdater = (function (exports, core) {
854
918
  reject(new Error('Failed to open popup'));
855
919
  return;
856
920
  }
857
- const cleanup = () => {
921
+ const cleanup = (shouldClose = false) => {
858
922
  window.removeEventListener('message', handleMessage);
859
923
  clearInterval(popupClosedInterval);
860
924
  clearTimeout(timeoutHandle);
861
925
  if (broadcastChannel) {
862
926
  broadcastChannel.close();
863
927
  }
928
+ if (shouldClose) {
929
+ try {
930
+ popup.close();
931
+ }
932
+ catch (_a) {
933
+ // Ignore cross-origin errors when closing the popup
934
+ }
935
+ }
864
936
  };
865
937
  const processOAuthResponse = (data) => {
866
938
  if (this.loginType === 'online') {
@@ -911,11 +983,11 @@ var capacitorCapacitorUpdater = (function (exports, core) {
911
983
  if (event.origin !== window.location.origin || ((_b = (_a = event.data) === null || _a === void 0 ? void 0 : _a.source) === null || _b === void 0 ? void 0 : _b.startsWith('angular')))
912
984
  return;
913
985
  if (((_c = event.data) === null || _c === void 0 ? void 0 : _c.type) === 'oauth-response') {
914
- cleanup();
986
+ cleanup(true);
915
987
  processOAuthResponse(event.data);
916
988
  }
917
989
  else if (((_d = event.data) === null || _d === void 0 ? void 0 : _d.type) === 'oauth-error') {
918
- cleanup();
990
+ cleanup(true);
919
991
  const errorMessage = event.data.error || 'User cancelled the OAuth flow';
920
992
  reject(new Error(errorMessage));
921
993
  }
@@ -929,11 +1001,11 @@ var capacitorCapacitorUpdater = (function (exports, core) {
929
1001
  if ((_a = data === null || data === void 0 ? void 0 : data.source) === null || _a === void 0 ? void 0 : _a.toString().startsWith('angular'))
930
1002
  return;
931
1003
  if ((data === null || data === void 0 ? void 0 : data.type) === 'oauth-response') {
932
- cleanup();
1004
+ cleanup(true);
933
1005
  processOAuthResponse(data);
934
1006
  }
935
1007
  else if ((data === null || data === void 0 ? void 0 : data.type) === 'oauth-error') {
936
- cleanup();
1008
+ cleanup(true);
937
1009
  const errorMessage = data.error || 'User cancelled the OAuth flow';
938
1010
  reject(new Error(errorMessage));
939
1011
  }
@@ -942,13 +1014,7 @@ var capacitorCapacitorUpdater = (function (exports, core) {
942
1014
  window.addEventListener('message', handleMessage);
943
1015
  // Timeout after 5 minutes
944
1016
  timeoutHandle = setTimeout(() => {
945
- cleanup();
946
- try {
947
- popup.close();
948
- }
949
- catch (_a) {
950
- // Ignore cross-origin errors when closing
951
- }
1017
+ cleanup(true);
952
1018
  reject(new Error('OAuth timeout'));
953
1019
  }, 300000);
954
1020
  popupClosedInterval = setInterval(() => {
@@ -976,10 +1042,13 @@ var capacitorCapacitorUpdater = (function (exports, core) {
976
1042
  */
977
1043
  class OAuth2SocialLogin extends BaseSocialLogin {
978
1044
  constructor() {
979
- super(...arguments);
1045
+ super();
980
1046
  this.providers = new Map();
981
1047
  this.TOKENS_KEY_PREFIX = 'capgo_social_login_oauth2_tokens_';
982
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();
983
1052
  }
984
1053
  normalizeScopeValue(scope) {
985
1054
  if (!scope)
@@ -1008,6 +1077,7 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1008
1077
  }
1009
1078
  return {
1010
1079
  appId,
1080
+ clientSecret: config.clientSecret,
1011
1081
  issuerUrl: config.issuerUrl,
1012
1082
  authorizationBaseUrl,
1013
1083
  accessTokenEndpoint,
@@ -1061,6 +1131,8 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1061
1131
  logoutUrl: config.logoutUrl,
1062
1132
  });
1063
1133
  }
1134
+ // Persist updated configuration after discovery
1135
+ this.persistConfiguration(providerId, config);
1064
1136
  }
1065
1137
  /**
1066
1138
  * Initialize multiple OAuth2 providers
@@ -1081,6 +1153,8 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1081
1153
  }
1082
1154
  // Pre-resolve discovery on web if issuerUrl is provided.
1083
1155
  await this.ensureDiscovered(providerId);
1156
+ // Persist configuration to localStorage so it's available in popup window context
1157
+ this.persistConfiguration(providerId, internalConfig);
1084
1158
  }
1085
1159
  }
1086
1160
  getProvider(providerId) {
@@ -1093,6 +1167,37 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1093
1167
  getTokensKey(providerId) {
1094
1168
  return `${this.TOKENS_KEY_PREFIX}${providerId}`;
1095
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
+ }
1096
1201
  async login(options) {
1097
1202
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1098
1203
  const { providerId } = options;
@@ -1470,6 +1575,9 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1470
1575
  if (config.pkceEnabled) {
1471
1576
  params.set('code_verifier', pending.codeVerifier);
1472
1577
  }
1578
+ if (config.clientSecret) {
1579
+ params.set('client_secret', config.clientSecret);
1580
+ }
1473
1581
  if (config.additionalTokenParameters) {
1474
1582
  for (const [k, v] of Object.entries(config.additionalTokenParameters)) {
1475
1583
  params.set(k, v);
@@ -1501,6 +1609,9 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1501
1609
  refresh_token: refreshToken,
1502
1610
  client_id: config.appId,
1503
1611
  });
1612
+ if (config.clientSecret) {
1613
+ params.set('client_secret', config.clientSecret);
1614
+ }
1504
1615
  if (config.additionalTokenParameters) {
1505
1616
  for (const [k, v] of Object.entries(config.additionalTokenParameters)) {
1506
1617
  params.set(k, v);
@@ -1662,6 +1773,7 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1662
1773
  const codeVerifier = (_e = options.codeVerifier) !== null && _e !== void 0 ? _e : this.generateCodeVerifier();
1663
1774
  const codeChallenge = await this.generateCodeChallenge(codeVerifier);
1664
1775
  this.persistPendingLogin(state, {
1776
+ clientId: this.clientId,
1665
1777
  codeVerifier,
1666
1778
  redirectUri,
1667
1779
  scopes,
@@ -1867,10 +1979,9 @@ var capacitorCapacitorUpdater = (function (exports, core) {
1867
1979
  }
1868
1980
  }
1869
1981
  async exchangeAuthorizationCode(code, pending) {
1870
- var _a;
1871
1982
  const params = new URLSearchParams({
1872
1983
  grant_type: 'authorization_code',
1873
- client_id: (_a = this.clientId) !== null && _a !== void 0 ? _a : '',
1984
+ client_id: pending.clientId,
1874
1985
  code,
1875
1986
  redirect_uri: pending.redirectUri,
1876
1987
  code_verifier: pending.codeVerifier,