isa-auth 0.2.2 → 0.2.3

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/index.mjs CHANGED
@@ -7840,20 +7840,32 @@ var require_jwtManager = __commonJS({
7840
7840
  return (0, _jwtUtils.isExpiredToken)(token, this.clockTolerance);
7841
7841
  }
7842
7842
  async verifyHmacSha256(token) {
7843
+ var _decoded_header, _globalThis_crypto;
7843
7844
  if (!this.secret || !token) return false;
7844
7845
  const parts = token.trim().split(".");
7845
7846
  if (parts.length !== 3) return false;
7846
- if (typeof window === "undefined" || !window.crypto || !window.crypto.subtle) {
7847
- return !this.isExpired(token);
7847
+ const decoded = (0, _jwtUtils.decodeToken)(token);
7848
+ if (!decoded || ((_decoded_header = decoded.header) === null || _decoded_header === void 0 ? void 0 : _decoded_header.alg) !== "HS256") return false;
7849
+ if (this.isExpired(token)) return false;
7850
+ const subtle = typeof globalThis !== "undefined" ? (_globalThis_crypto = globalThis.crypto) === null || _globalThis_crypto === void 0 ? void 0 : _globalThis_crypto.subtle : void 0;
7851
+ if (!subtle) {
7852
+ return false;
7848
7853
  }
7849
7854
  try {
7850
7855
  const enc = new TextEncoder();
7851
- const key = await window.crypto.subtle.importKey("raw", enc.encode(this.secret), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
7856
+ const key = await subtle.importKey("raw", enc.encode(this.secret), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
7852
7857
  let sigBase64 = parts[2].replace(/-/g, "+").replace(/_/g, "/");
7853
7858
  while (sigBase64.length % 4 !== 0) sigBase64 += "=";
7854
- const sigBytes = Uint8Array.from(atob(sigBase64), (c) => c.charCodeAt(0));
7859
+ let sigBytes;
7860
+ if (typeof atob === "function") {
7861
+ sigBytes = Uint8Array.from(atob(sigBase64), (c) => c.charCodeAt(0));
7862
+ } else if (typeof globalThis.Buffer !== "undefined") {
7863
+ sigBytes = new Uint8Array(globalThis.Buffer.from(sigBase64, "base64"));
7864
+ } else {
7865
+ return false;
7866
+ }
7855
7867
  const dataBytes = enc.encode(`${parts[0]}.${parts[1]}`);
7856
- return await window.crypto.subtle.verify("HMAC", key, sigBytes, dataBytes);
7868
+ return await subtle.verify("HMAC", key, sigBytes, dataBytes);
7857
7869
  } catch {
7858
7870
  return false;
7859
7871
  }
@@ -7959,13 +7971,14 @@ var require_guard = __commonJS({
7959
7971
  if (userPerm === "*" || pattern === "*" || userPerm === pattern) {
7960
7972
  return true;
7961
7973
  }
7962
- const regexStr = "^" + pattern.replace(/\./g, "\\.").replace(/\*/g, ".*") + "$";
7974
+ const escapeRegexExceptWildcard = (str) => str.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
7975
+ const regexStr = "^" + escapeRegexExceptWildcard(pattern) + "$";
7963
7976
  try {
7964
7977
  const regex = new RegExp(regexStr);
7965
7978
  if (regex.test(userPerm)) return true;
7966
7979
  } catch {
7967
7980
  }
7968
- const userRegexStr = "^" + userPerm.replace(/\./g, "\\.").replace(/\*/g, ".*") + "$";
7981
+ const userRegexStr = "^" + escapeRegexExceptWildcard(userPerm) + "$";
7969
7982
  try {
7970
7983
  const userRegex = new RegExp(userRegexStr);
7971
7984
  if (userRegex.test(pattern)) return true;
@@ -8128,7 +8141,10 @@ var require_jwtService = __commonJS({
8128
8141
  (0, _store.setAccessToken)(value);
8129
8142
  if (value) {
8130
8143
  const payload = this.getAccessTokenPayload();
8131
- this.setAuthInfo(payload);
8144
+ if (payload) {
8145
+ const currentInfo = this.getAuthInfo() || {};
8146
+ this.setAuthInfo({ ...currentInfo, ...payload });
8147
+ }
8132
8148
  this.setAuthState(true);
8133
8149
  if (this.autoRefreshFn) {
8134
8150
  this.scheduleNextAutoRefresh();
@@ -8387,6 +8403,229 @@ var require_service = __commonJS({
8387
8403
  }
8388
8404
  });
8389
8405
 
8406
+ // ../core/dist/providers/pkce.js
8407
+ var require_pkce = __commonJS({
8408
+ "../core/dist/providers/pkce.js"(exports) {
8409
+ "use strict";
8410
+ Object.defineProperty(exports, "__esModule", { value: true });
8411
+ function _export(target, all) {
8412
+ for (var name in all) Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get });
8413
+ }
8414
+ _export(exports, { get buildAuthorizeUrl() {
8415
+ return buildAuthorizeUrl2;
8416
+ }, get generateCodeChallenge() {
8417
+ return generateCodeChallenge2;
8418
+ }, get generateCodeVerifier() {
8419
+ return generateCodeVerifier2;
8420
+ }, get generateRandomState() {
8421
+ return generateRandomState2;
8422
+ }, get handleOAuthCallback() {
8423
+ return handleOAuthCallback2;
8424
+ }, get openOAuthPopup() {
8425
+ return openOAuthPopup2;
8426
+ }, get parseOAuthCallback() {
8427
+ return parseOAuthCallback2;
8428
+ }, get waitForOAuthPopupMessage() {
8429
+ return waitForOAuthPopupMessage2;
8430
+ } });
8431
+ var _jwtUtils = require_jwtUtils();
8432
+ function generateRandomState2(length = 32) {
8433
+ const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
8434
+ const array = new Uint8Array(length);
8435
+ if (typeof globalThis !== "undefined" && globalThis.crypto && typeof globalThis.crypto.getRandomValues === "function") {
8436
+ globalThis.crypto.getRandomValues(array);
8437
+ } else {
8438
+ for (let i = 0; i < length; i++) {
8439
+ array[i] = Math.floor(Math.random() * 256);
8440
+ }
8441
+ }
8442
+ let result = "";
8443
+ for (let i = 0; i < length; i++) {
8444
+ result += charset[array[i] % charset.length];
8445
+ }
8446
+ return result;
8447
+ }
8448
+ function generateCodeVerifier2(length = 64) {
8449
+ const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
8450
+ const array = new Uint8Array(length);
8451
+ if (typeof window !== "undefined" && window.crypto) {
8452
+ window.crypto.getRandomValues(array);
8453
+ } else {
8454
+ for (let i = 0; i < length; i++) {
8455
+ array[i] = Math.floor(Math.random() * 256);
8456
+ }
8457
+ }
8458
+ let result = "";
8459
+ for (let i = 0; i < length; i++) {
8460
+ result += charset[array[i] % charset.length];
8461
+ }
8462
+ return result;
8463
+ }
8464
+ async function generateCodeChallenge2(verifier) {
8465
+ if (typeof window !== "undefined" && window.crypto && window.crypto.subtle) {
8466
+ const encoder = new TextEncoder();
8467
+ const data = encoder.encode(verifier);
8468
+ const digest = await window.crypto.subtle.digest("SHA-256", data);
8469
+ const bytes = new Uint8Array(digest);
8470
+ let binary = "";
8471
+ for (let i = 0; i < bytes.byteLength; i++) {
8472
+ binary += String.fromCharCode(bytes[i]);
8473
+ }
8474
+ const base64 = btoa(binary);
8475
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
8476
+ }
8477
+ return (0, _jwtUtils.base64UrlEncode)(verifier);
8478
+ }
8479
+ function buildAuthorizeUrl2(options) {
8480
+ const { authorizeUrl, clientId, redirectUri, responseType = "code", scope, state, codeChallenge, codeChallengeMethod = "S256", extraParams = {} } = options;
8481
+ const url = new URL(authorizeUrl);
8482
+ url.searchParams.set("client_id", clientId);
8483
+ url.searchParams.set("redirect_uri", redirectUri);
8484
+ url.searchParams.set("response_type", responseType);
8485
+ if (scope) url.searchParams.set("scope", scope);
8486
+ if (state) url.searchParams.set("state", state);
8487
+ if (codeChallenge) {
8488
+ url.searchParams.set("code_challenge", codeChallenge);
8489
+ url.searchParams.set("code_challenge_method", codeChallengeMethod);
8490
+ }
8491
+ for (const [key, value] of Object.entries(extraParams)) {
8492
+ url.searchParams.set(key, value);
8493
+ }
8494
+ return url.toString();
8495
+ }
8496
+ function parseOAuthCallback2(urlOrSearch) {
8497
+ const result = {};
8498
+ const parseString = (str) => {
8499
+ if (!str) return;
8500
+ const cleaned = str.replace(/^[?#&]+/, "");
8501
+ if (!cleaned) return;
8502
+ const params = new URLSearchParams(cleaned);
8503
+ params.forEach((val, key) => {
8504
+ result[key] = val;
8505
+ });
8506
+ };
8507
+ if (urlOrSearch) {
8508
+ if (/^https?:\/\//i.test(urlOrSearch)) {
8509
+ try {
8510
+ const parsedUrl = new URL(urlOrSearch);
8511
+ if (parsedUrl.search) {
8512
+ parseString(parsedUrl.search);
8513
+ }
8514
+ if (parsedUrl.hash) {
8515
+ parseString(parsedUrl.hash);
8516
+ }
8517
+ } catch {
8518
+ }
8519
+ } else if (urlOrSearch.includes("?")) {
8520
+ const parts = urlOrSearch.split("?");
8521
+ const queryAndHash = parts[1] || "";
8522
+ if (queryAndHash.includes("#")) {
8523
+ const [query, hash] = queryAndHash.split("#");
8524
+ parseString(query);
8525
+ parseString(hash);
8526
+ } else {
8527
+ parseString(queryAndHash);
8528
+ }
8529
+ } else if (urlOrSearch.includes("#")) {
8530
+ parseString(urlOrSearch.split("#")[1]);
8531
+ } else {
8532
+ parseString(urlOrSearch);
8533
+ }
8534
+ } else if (typeof window !== "undefined") {
8535
+ if (window.location.search) {
8536
+ parseString(window.location.search);
8537
+ }
8538
+ if (window.location.hash) {
8539
+ parseString(window.location.hash);
8540
+ }
8541
+ }
8542
+ if (result.access_token && !result.token) {
8543
+ result.token = result.access_token;
8544
+ }
8545
+ return result;
8546
+ }
8547
+ function handleOAuthCallback2(options) {
8548
+ const result = parseOAuthCallback2(options === null || options === void 0 ? void 0 : options.urlOrSearch);
8549
+ if (typeof window !== "undefined") {
8550
+ if ((options === null || options === void 0 ? void 0 : options.storageKey) && window.sessionStorage) {
8551
+ try {
8552
+ window.sessionStorage.setItem(options.storageKey, JSON.stringify(result));
8553
+ } catch (e) {
8554
+ }
8555
+ }
8556
+ if (window.opener && typeof window.opener.postMessage === "function") {
8557
+ const targetOrigin = (options === null || options === void 0 ? void 0 : options.targetOrigin) || window.location.origin;
8558
+ window.opener.postMessage({ type: "ISA_AUTH_OAUTH_CALLBACK", payload: result }, targetOrigin);
8559
+ if ((options === null || options === void 0 ? void 0 : options.closePopup) !== false) {
8560
+ window.close();
8561
+ }
8562
+ }
8563
+ }
8564
+ return result;
8565
+ }
8566
+ function openOAuthPopup2(url, title = "isa_auth_oauth_login", width = 500, height = 650) {
8567
+ if (typeof window === "undefined") return null;
8568
+ const left = window.screenX + Math.max(0, (window.outerWidth - width) / 2);
8569
+ const top = window.screenY + Math.max(0, (window.outerHeight - height) / 2);
8570
+ const popup = window.open(url, title, `width=${width},height=${height},left=${left},top=${top},status=no,resizable=yes,scrollbars=yes`);
8571
+ return popup;
8572
+ }
8573
+ function waitForOAuthPopupMessage2(popup, timeoutMsOrOptions = 12e4) {
8574
+ const opts = typeof timeoutMsOrOptions === "number" ? { timeoutMs: timeoutMsOrOptions } : timeoutMsOrOptions || {};
8575
+ var _opts_timeoutMs;
8576
+ const timeoutMs = (_opts_timeoutMs = opts.timeoutMs) !== null && _opts_timeoutMs !== void 0 ? _opts_timeoutMs : 12e4;
8577
+ var _opts_expectedOrigin;
8578
+ const expectedOrigin = (_opts_expectedOrigin = opts.expectedOrigin) !== null && _opts_expectedOrigin !== void 0 ? _opts_expectedOrigin : typeof window !== "undefined" && window.location ? window.location.origin : "*";
8579
+ const expectedState = opts.expectedState;
8580
+ return new Promise((resolve, reject) => {
8581
+ if (typeof window === "undefined") {
8582
+ return reject(new Error("Window is undefined"));
8583
+ }
8584
+ if (!popup) {
8585
+ return reject(new Error("Popup blocked or failed to open"));
8586
+ }
8587
+ let timer = null;
8588
+ let checkClosedInterval = null;
8589
+ const cleanup = () => {
8590
+ if (timer) clearTimeout(timer);
8591
+ if (checkClosedInterval) clearInterval(checkClosedInterval);
8592
+ window.removeEventListener("message", handleMessage);
8593
+ };
8594
+ const handleMessage = (event) => {
8595
+ if (expectedOrigin !== "*" && event.origin !== expectedOrigin) {
8596
+ return;
8597
+ }
8598
+ if (event.source && event.source !== popup) {
8599
+ return;
8600
+ }
8601
+ if (event.data && event.data.type === "ISA_AUTH_OAUTH_CALLBACK") {
8602
+ cleanup();
8603
+ const payload = event.data.payload || {};
8604
+ if (expectedState && payload.state !== expectedState) {
8605
+ reject(new Error("OAuth state mismatch (possible CSRF attack)"));
8606
+ } else if (payload.error) {
8607
+ reject(new Error(payload.error_description || payload.error));
8608
+ } else {
8609
+ resolve(payload);
8610
+ }
8611
+ }
8612
+ };
8613
+ window.addEventListener("message", handleMessage);
8614
+ checkClosedInterval = setInterval(() => {
8615
+ if (popup.closed) {
8616
+ cleanup();
8617
+ reject(new Error("Popup was closed by user before completing authentication"));
8618
+ }
8619
+ }, 500);
8620
+ timer = setTimeout(() => {
8621
+ cleanup();
8622
+ reject(new Error(`OAuth authentication timed out after ${timeoutMs}ms`));
8623
+ }, timeoutMs);
8624
+ });
8625
+ }
8626
+ }
8627
+ });
8628
+
8390
8629
  // ../core/dist/providers/social/googleAuth.js
8391
8630
  var require_googleAuth = __commonJS({
8392
8631
  "../core/dist/providers/social/googleAuth.js"(exports) {
@@ -8401,6 +8640,8 @@ var require_googleAuth = __commonJS({
8401
8640
  return _default;
8402
8641
  } });
8403
8642
  var _isautil = require_dist();
8643
+ var _jwtUtils = require_jwtUtils();
8644
+ var _pkce = require_pkce();
8404
8645
  function _define_property(obj, key, value) {
8405
8646
  if (key in obj) {
8406
8647
  Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
@@ -8411,29 +8652,128 @@ var require_googleAuth = __commonJS({
8411
8652
  }
8412
8653
  var GoogleAuthProvider2 = class {
8413
8654
  async loadGoogleSdk() {
8414
- var _window_google_accounts;
8415
- if (typeof window !== "undefined" && window.google && ((_window_google_accounts = window.google.accounts) === null || _window_google_accounts === void 0 ? void 0 : _window_google_accounts.id)) {
8655
+ if (typeof window !== "undefined" && window.google && window.google.accounts) {
8416
8656
  return;
8417
8657
  }
8418
8658
  await (0, _isautil.loadCDN)("google-gsi", "https://accounts.google.com/gsi/client", { async: true, defer: true });
8419
8659
  }
8420
8660
  async init() {
8421
- await this.loadGoogleSdk();
8422
- if (typeof window !== "undefined" && window.google) {
8661
+ var _window_google_accounts, _window_google, _window_google_accounts1, _window_google1;
8662
+ if (typeof window === "undefined") return;
8663
+ try {
8664
+ await this.loadGoogleSdk();
8665
+ } catch {
8666
+ return;
8667
+ }
8668
+ if ((_window_google = window.google) === null || _window_google === void 0 ? void 0 : (_window_google_accounts = _window_google.accounts) === null || _window_google_accounts === void 0 ? void 0 : _window_google_accounts.id) {
8423
8669
  window.google.accounts.id.initialize({ client_id: this.clientId, callback: (response) => {
8424
- this.callback(response.credential);
8670
+ if (response === null || response === void 0 ? void 0 : response.credential) {
8671
+ this.handleCredentialResponse(response.credential);
8672
+ }
8425
8673
  } });
8674
+ if (this.autoPrompt) {
8675
+ window.google.accounts.id.prompt();
8676
+ }
8677
+ }
8678
+ if ((_window_google1 = window.google) === null || _window_google1 === void 0 ? void 0 : (_window_google_accounts1 = _window_google1.accounts) === null || _window_google_accounts1 === void 0 ? void 0 : _window_google_accounts1.oauth2) {
8679
+ try {
8680
+ this.tokenClient = window.google.accounts.oauth2.initTokenClient({ client_id: this.clientId, scope: this.scope, callback: async (tokenResponse) => {
8681
+ const pending = this.pendingSignIn;
8682
+ this.pendingSignIn = null;
8683
+ if (tokenResponse === null || tokenResponse === void 0 ? void 0 : tokenResponse.error) {
8684
+ if (this.onFail) this.onFail(tokenResponse);
8685
+ if (pending) pending.reject(tokenResponse);
8686
+ return;
8687
+ }
8688
+ this.accessToken = tokenResponse.access_token;
8689
+ try {
8690
+ const res = await fetch("https://www.googleapis.com/oauth2/v3/userinfo", { headers: { Authorization: `Bearer ${tokenResponse.access_token}` } });
8691
+ if (res.ok) {
8692
+ this.userInfo = await res.json();
8693
+ }
8694
+ } catch (e) {
8695
+ }
8696
+ if (this.callback) await this.callback(tokenResponse.access_token);
8697
+ if (this.onSuccess) this.onSuccess(this.userInfo || tokenResponse);
8698
+ if (pending) pending.resolve();
8699
+ }, error_callback: (err) => {
8700
+ const pending = this.pendingSignIn;
8701
+ this.pendingSignIn = null;
8702
+ if (this.onFail) this.onFail(err);
8703
+ if (pending) pending.reject(err);
8704
+ } });
8705
+ } catch (err) {
8706
+ }
8426
8707
  }
8427
8708
  }
8428
- signIn() {
8429
- if (typeof window !== "undefined" && window.google) {
8709
+ handleCredentialResponse(credential) {
8710
+ this.idToken = credential;
8711
+ const decoded = (0, _jwtUtils.decodeToken)(credential);
8712
+ if (decoded === null || decoded === void 0 ? void 0 : decoded.payload) {
8713
+ this.userInfo = decoded.payload;
8714
+ }
8715
+ if (this.callback) {
8716
+ this.callback(credential);
8717
+ }
8718
+ if (this.onSuccess) {
8719
+ this.onSuccess(this.userInfo || { id_token: credential });
8720
+ }
8721
+ }
8722
+ promptOneTap() {
8723
+ var _window_google_accounts, _window_google;
8724
+ if (typeof window !== "undefined" && ((_window_google = window.google) === null || _window_google === void 0 ? void 0 : (_window_google_accounts = _window_google.accounts) === null || _window_google_accounts === void 0 ? void 0 : _window_google_accounts.id)) {
8430
8725
  window.google.accounts.id.prompt();
8726
+ }
8727
+ }
8728
+ renderGoogleButton(parent, options) {
8729
+ var _window_google_accounts, _window_google;
8730
+ if (typeof window !== "undefined" && ((_window_google = window.google) === null || _window_google === void 0 ? void 0 : (_window_google_accounts = _window_google.accounts) === null || _window_google_accounts === void 0 ? void 0 : _window_google_accounts.id)) {
8731
+ window.google.accounts.id.renderButton(parent, options || { theme: "outline", size: "large" });
8732
+ }
8733
+ }
8734
+ async signIn() {
8735
+ var _window_google_accounts, _window_google;
8736
+ if (typeof window === "undefined") return;
8737
+ if (this.tokenClient && typeof this.tokenClient.requestAccessToken === "function") {
8738
+ return new Promise((resolve, reject) => {
8739
+ this.pendingSignIn = { resolve, reject };
8740
+ this.tokenClient.requestAccessToken({ prompt: "consent" });
8741
+ });
8742
+ }
8743
+ if ((_window_google = window.google) === null || _window_google === void 0 ? void 0 : (_window_google_accounts = _window_google.accounts) === null || _window_google_accounts === void 0 ? void 0 : _window_google_accounts.id) {
8744
+ window.google.accounts.id.prompt();
8745
+ return;
8746
+ }
8747
+ const state = (0, _pkce.generateRandomState)();
8748
+ const authUrl = (0, _pkce.buildAuthorizeUrl)({ authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth", clientId: this.clientId, redirectUri: this.redirectUri || window.location.origin, responseType: "token id_token", scope: this.scope, state, extraParams: { nonce: (0, _pkce.generateRandomState)() } });
8749
+ if (this.isPopup) {
8750
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "google_login");
8751
+ if (!popup) {
8752
+ window.location.href = authUrl;
8753
+ return;
8754
+ }
8755
+ try {
8756
+ const callbackResult = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: state });
8757
+ if (callbackResult.access_token) {
8758
+ this.accessToken = callbackResult.access_token;
8759
+ }
8760
+ if (callbackResult.id_token) {
8761
+ this.handleCredentialResponse(callbackResult.id_token);
8762
+ }
8763
+ } catch (err) {
8764
+ if (this.onFail) this.onFail(err);
8765
+ }
8431
8766
  } else {
8432
- console.error("Google SDK is not loaded");
8767
+ window.location.href = authUrl;
8433
8768
  }
8434
8769
  }
8435
8770
  signOut() {
8436
8771
  var _window_google;
8772
+ const tokenToRevoke = this.accessToken;
8773
+ this.accessToken = null;
8774
+ this.idToken = null;
8775
+ this.userInfo = null;
8776
+ this.pendingSignIn = null;
8437
8777
  if (typeof window !== "undefined" && ((_window_google = window.google) === null || _window_google === void 0 ? void 0 : _window_google.accounts)) {
8438
8778
  try {
8439
8779
  var _window_google_accounts_id;
@@ -8444,38 +8784,45 @@ var require_googleAuth = __commonJS({
8444
8784
  }
8445
8785
  try {
8446
8786
  var _window_google_accounts_oauth2;
8447
- if (typeof ((_window_google_accounts_oauth2 = window.google.accounts.oauth2) === null || _window_google_accounts_oauth2 === void 0 ? void 0 : _window_google_accounts_oauth2.revokeAccessToken) === "function") {
8448
- window.google.accounts.oauth2.revokeAccessToken();
8787
+ if (typeof ((_window_google_accounts_oauth2 = window.google.accounts.oauth2) === null || _window_google_accounts_oauth2 === void 0 ? void 0 : _window_google_accounts_oauth2.revoke) === "function" && tokenToRevoke) {
8788
+ window.google.accounts.oauth2.revoke(tokenToRevoke, () => {
8789
+ });
8449
8790
  }
8450
8791
  } catch (e) {
8451
8792
  }
8452
8793
  }
8453
8794
  }
8454
8795
  async getAccessToken() {
8455
- var _window_google_accounts, _window_google;
8456
- if (typeof window !== "undefined" && ((_window_google = window.google) === null || _window_google === void 0 ? void 0 : (_window_google_accounts = _window_google.accounts) === null || _window_google_accounts === void 0 ? void 0 : _window_google_accounts.oauth2)) {
8457
- if (typeof window.google.accounts.oauth2.getAccessToken === "function") {
8458
- const token = await window.google.accounts.oauth2.getAccessToken();
8459
- return token;
8460
- }
8461
- }
8462
- return null;
8796
+ return this.accessToken || this.idToken || null;
8463
8797
  }
8464
8798
  async getUserInfo() {
8465
- var _window_google_accounts, _window_google;
8466
- if (typeof window !== "undefined" && ((_window_google = window.google) === null || _window_google === void 0 ? void 0 : (_window_google_accounts = _window_google.accounts) === null || _window_google_accounts === void 0 ? void 0 : _window_google_accounts.id)) {
8467
- if (typeof window.google.accounts.id.getUserInfo === "function") {
8468
- const user = await window.google.accounts.id.getUserInfo();
8469
- return user;
8470
- }
8471
- }
8472
- return null;
8799
+ return this.userInfo || null;
8473
8800
  }
8474
- constructor({ clientId, callback }) {
8801
+ constructor(options) {
8475
8802
  _define_property(this, "clientId", void 0);
8476
8803
  _define_property(this, "callback", void 0);
8477
- this.clientId = clientId;
8478
- this.callback = callback;
8804
+ _define_property(this, "scope", void 0);
8805
+ _define_property(this, "autoPrompt", void 0);
8806
+ _define_property(this, "redirectUri", void 0);
8807
+ _define_property(this, "isPopup", void 0);
8808
+ _define_property(this, "onSuccess", void 0);
8809
+ _define_property(this, "onFail", void 0);
8810
+ _define_property(this, "idToken", null);
8811
+ _define_property(this, "accessToken", null);
8812
+ _define_property(this, "userInfo", null);
8813
+ _define_property(this, "tokenClient", null);
8814
+ _define_property(this, "pendingSignIn", null);
8815
+ this.clientId = options.clientId;
8816
+ this.callback = options.callback;
8817
+ const opts = options;
8818
+ this.scope = opts.scope || "openid email profile";
8819
+ var _opts_autoPrompt;
8820
+ this.autoPrompt = (_opts_autoPrompt = opts.autoPrompt) !== null && _opts_autoPrompt !== void 0 ? _opts_autoPrompt : false;
8821
+ this.redirectUri = opts.redirectUri;
8822
+ var _opts_isPopup;
8823
+ this.isPopup = (_opts_isPopup = opts.isPopup) !== null && _opts_isPopup !== void 0 ? _opts_isPopup : true;
8824
+ this.onSuccess = opts.onSuccess;
8825
+ this.onFail = opts.onFail;
8479
8826
  }
8480
8827
  };
8481
8828
  var _default = GoogleAuthProvider2;
@@ -8496,6 +8843,8 @@ var require_appleAuth = __commonJS({
8496
8843
  return _default;
8497
8844
  } });
8498
8845
  var _isautil = require_dist();
8846
+ var _jwtUtils = require_jwtUtils();
8847
+ var _pkce = require_pkce();
8499
8848
  function _define_property(obj, key, value) {
8500
8849
  if (key in obj) {
8501
8850
  Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
@@ -8513,149 +8862,118 @@ var require_appleAuth = __commonJS({
8513
8862
  }
8514
8863
  async init() {
8515
8864
  if (typeof window === "undefined") return;
8516
- await this.loadAppleSdk();
8517
- if (window.AppleID && window.AppleID.auth) {
8518
- window.AppleID.auth.init({ clientId: this.clientId, scope: this.scope, redirectURI: this.redirectURI, state: this.state || Math.random().toString(36).substring(2, 15), nonce: this.nonce, usePopup: this.usePopup });
8865
+ try {
8866
+ await this.loadAppleSdk();
8867
+ if (window.AppleID && window.AppleID.auth) {
8868
+ window.AppleID.auth.init({ clientId: this.clientId, scope: this.scope, redirectURI: this.redirectURI, state: this.state || Math.random().toString(36).substring(2, 15), nonce: this.nonce, usePopup: this.usePopup });
8869
+ }
8870
+ } catch (e) {
8519
8871
  }
8520
8872
  }
8873
+ buildAuthUrl() {
8874
+ const state = this.state || (0, _pkce.generateRandomState)();
8875
+ this.lastState = state;
8876
+ const requestsUserFields = /\b(name|email)\b/.test(this.scope);
8877
+ const responseMode = this.responseMode || (requestsUserFields ? "form_post" : "fragment");
8878
+ return (0, _pkce.buildAuthorizeUrl)({ authorizeUrl: "https://appleid.apple.com/auth/authorize", clientId: this.clientId, redirectUri: this.redirectURI, responseType: "code id_token", scope: this.scope, state, extraParams: { response_mode: responseMode, ...this.nonce ? { nonce: this.nonce } : {} } });
8879
+ }
8521
8880
  async signIn() {
8522
8881
  if (typeof window === "undefined") return;
8523
- if (!window.AppleID || !window.AppleID.auth) {
8524
- console.error("[ISA-AUTH] Apple Sign-In SDK is not loaded");
8525
- return;
8526
- }
8527
- try {
8528
- const response = await window.AppleID.auth.signIn();
8529
- if (response && response.authorization) {
8530
- this.idToken = response.authorization.id_token;
8531
- this.userInfo = response.user || null;
8532
- if (this.onSuccess) {
8533
- this.onSuccess(response);
8882
+ if (window.AppleID && window.AppleID.auth) {
8883
+ try {
8884
+ const response = await window.AppleID.auth.signIn();
8885
+ if (response && response.authorization) {
8886
+ this.idToken = response.authorization.id_token;
8887
+ this.userInfo = response.user || null;
8888
+ if (!this.userInfo && this.idToken) {
8889
+ const decoded = (0, _jwtUtils.decodeToken)(this.idToken);
8890
+ if (decoded === null || decoded === void 0 ? void 0 : decoded.payload) {
8891
+ this.userInfo = decoded.payload;
8892
+ }
8893
+ }
8894
+ if (this.onSuccess) {
8895
+ this.onSuccess(response);
8896
+ }
8897
+ }
8898
+ return;
8899
+ } catch (error) {
8900
+ console.error("[ISA-AUTH] Apple sign-in failed:", error);
8901
+ if (this.onFail) {
8902
+ this.onFail(error);
8534
8903
  }
8904
+ return;
8535
8905
  }
8536
- } catch (error) {
8537
- console.error("[ISA-AUTH] Apple sign-in failed:", error);
8538
- if (this.onFail) {
8539
- this.onFail(error);
8906
+ }
8907
+ const authUrl = this.buildAuthUrl();
8908
+ if (this.usePopup) {
8909
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "apple_login", 500, 650);
8910
+ if (!popup) {
8911
+ window.location.href = authUrl;
8912
+ return;
8540
8913
  }
8914
+ try {
8915
+ const result = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: this.lastState });
8916
+ if (result.id_token) {
8917
+ this.idToken = result.id_token;
8918
+ const decoded = (0, _jwtUtils.decodeToken)(result.id_token);
8919
+ if (decoded === null || decoded === void 0 ? void 0 : decoded.payload) {
8920
+ this.userInfo = decoded.payload;
8921
+ }
8922
+ }
8923
+ if (result.code && !this.idToken) {
8924
+ this.idToken = result.code;
8925
+ }
8926
+ if (this.onSuccess) this.onSuccess(result);
8927
+ } catch (err) {
8928
+ if (this.onFail) this.onFail(err);
8929
+ }
8930
+ } else {
8931
+ window.location.href = authUrl;
8541
8932
  }
8542
8933
  }
8543
8934
  signOut() {
8544
8935
  this.idToken = null;
8545
8936
  this.userInfo = null;
8546
8937
  }
8547
- async getAccessToken() {
8548
- return this.idToken;
8549
- }
8550
- async getUserInfo() {
8551
- return this.userInfo;
8552
- }
8553
- constructor(options) {
8554
- _define_property(this, "clientId", void 0);
8555
- _define_property(this, "redirectURI", void 0);
8556
- _define_property(this, "scope", void 0);
8557
- _define_property(this, "state", void 0);
8558
- _define_property(this, "nonce", void 0);
8559
- _define_property(this, "usePopup", void 0);
8560
- _define_property(this, "onSuccess", void 0);
8561
- _define_property(this, "onFail", void 0);
8562
- _define_property(this, "idToken", null);
8563
- _define_property(this, "userInfo", null);
8564
- this.clientId = options.clientId;
8565
- this.redirectURI = options.redirectURI || (typeof window !== "undefined" ? window.location.origin : "");
8566
- this.scope = options.scope || "name email";
8567
- this.state = options.state;
8568
- this.nonce = options.nonce;
8569
- var _options_usePopup;
8570
- this.usePopup = (_options_usePopup = options.usePopup) !== null && _options_usePopup !== void 0 ? _options_usePopup : true;
8571
- this.onSuccess = options.onSuccess;
8572
- this.onFail = options.onFail;
8573
- }
8574
- };
8575
- var _default = AppleAuthProvider2;
8576
- }
8577
- });
8578
-
8579
- // ../core/dist/providers/pkce.js
8580
- var require_pkce = __commonJS({
8581
- "../core/dist/providers/pkce.js"(exports) {
8582
- "use strict";
8583
- Object.defineProperty(exports, "__esModule", { value: true });
8584
- function _export(target, all) {
8585
- for (var name in all) Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get });
8586
- }
8587
- _export(exports, { get buildAuthorizeUrl() {
8588
- return buildAuthorizeUrl2;
8589
- }, get generateCodeChallenge() {
8590
- return generateCodeChallenge2;
8591
- }, get generateCodeVerifier() {
8592
- return generateCodeVerifier2;
8593
- }, get parseOAuthCallback() {
8594
- return parseOAuthCallback2;
8595
- } });
8596
- var _jwtUtils = require_jwtUtils();
8597
- function generateCodeVerifier2(length = 64) {
8598
- const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
8599
- const array = new Uint8Array(length);
8600
- if (typeof window !== "undefined" && window.crypto) {
8601
- window.crypto.getRandomValues(array);
8602
- } else {
8603
- for (let i = 0; i < length; i++) {
8604
- array[i] = Math.floor(Math.random() * 256);
8605
- }
8606
- }
8607
- let result = "";
8608
- for (let i = 0; i < length; i++) {
8609
- result += charset[array[i] % charset.length];
8610
- }
8611
- return result;
8612
- }
8613
- async function generateCodeChallenge2(verifier) {
8614
- if (typeof window !== "undefined" && window.crypto && window.crypto.subtle) {
8615
- const encoder = new TextEncoder();
8616
- const data = encoder.encode(verifier);
8617
- const digest = await window.crypto.subtle.digest("SHA-256", data);
8618
- const bytes = new Uint8Array(digest);
8619
- let binary = "";
8620
- for (let i = 0; i < bytes.byteLength; i++) {
8621
- binary += String.fromCharCode(bytes[i]);
8622
- }
8623
- const base64 = btoa(binary);
8624
- return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
8938
+ async getAccessToken() {
8939
+ return this.idToken;
8625
8940
  }
8626
- return (0, _jwtUtils.base64UrlEncode)(verifier);
8627
- }
8628
- function buildAuthorizeUrl2(options) {
8629
- const { authorizeUrl, clientId, redirectUri, responseType = "code", scope, state, codeChallenge, codeChallengeMethod = "S256", extraParams = {} } = options;
8630
- const url = new URL(authorizeUrl);
8631
- url.searchParams.set("client_id", clientId);
8632
- url.searchParams.set("redirect_uri", redirectUri);
8633
- url.searchParams.set("response_type", responseType);
8634
- if (scope) url.searchParams.set("scope", scope);
8635
- if (state) url.searchParams.set("state", state);
8636
- if (codeChallenge) {
8637
- url.searchParams.set("code_challenge", codeChallenge);
8638
- url.searchParams.set("code_challenge_method", codeChallengeMethod);
8941
+ async getUserInfo() {
8942
+ return this.userInfo;
8639
8943
  }
8640
- for (const [key, value] of Object.entries(extraParams)) {
8641
- url.searchParams.set(key, value);
8944
+ setToken(token) {
8945
+ this.idToken = token;
8642
8946
  }
8643
- return url.toString();
8644
- }
8645
- function parseOAuthCallback2(urlOrSearch) {
8646
- let search = "";
8647
- if (urlOrSearch) {
8648
- search = urlOrSearch.includes("?") ? urlOrSearch.split("?")[1] : urlOrSearch;
8649
- } else if (typeof window !== "undefined") {
8650
- search = window.location.search.slice(1);
8947
+ setUser(user) {
8948
+ this.userInfo = user;
8949
+ if (this.onSuccess) this.onSuccess(user);
8651
8950
  }
8652
- const params = new URLSearchParams(search);
8653
- const result = {};
8654
- params.forEach((val, key) => {
8655
- result[key] = val;
8656
- });
8657
- return result;
8658
- }
8951
+ constructor(options) {
8952
+ _define_property(this, "clientId", void 0);
8953
+ _define_property(this, "redirectURI", void 0);
8954
+ _define_property(this, "scope", void 0);
8955
+ _define_property(this, "state", void 0);
8956
+ _define_property(this, "lastState", void 0);
8957
+ _define_property(this, "nonce", void 0);
8958
+ _define_property(this, "usePopup", void 0);
8959
+ _define_property(this, "responseMode", void 0);
8960
+ _define_property(this, "onSuccess", void 0);
8961
+ _define_property(this, "onFail", void 0);
8962
+ _define_property(this, "idToken", null);
8963
+ _define_property(this, "userInfo", null);
8964
+ this.clientId = options.clientId;
8965
+ this.redirectURI = options.redirectURI || (typeof window !== "undefined" ? window.location.origin : "");
8966
+ this.scope = options.scope !== void 0 ? options.scope : "name email";
8967
+ this.state = options.state;
8968
+ this.nonce = options.nonce;
8969
+ var _options_usePopup;
8970
+ this.usePopup = (_options_usePopup = options.usePopup) !== null && _options_usePopup !== void 0 ? _options_usePopup : true;
8971
+ this.responseMode = options.responseMode;
8972
+ this.onSuccess = options.onSuccess;
8973
+ this.onFail = options.onFail;
8974
+ }
8975
+ };
8976
+ var _default = AppleAuthProvider2;
8659
8977
  }
8660
8978
  });
8661
8979
 
@@ -8668,7 +8986,7 @@ var require_metaAuth = __commonJS({
8668
8986
  for (var name in all) Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get });
8669
8987
  }
8670
8988
  _export(exports, { get FacebookAuthProvider() {
8671
- return FacebookAuthProvider2;
8989
+ return MetaAuthProvider2;
8672
8990
  }, get MetaAuthProvider() {
8673
8991
  return MetaAuthProvider2;
8674
8992
  }, get default() {
@@ -8693,57 +9011,108 @@ var require_metaAuth = __commonJS({
8693
9011
  }
8694
9012
  async init() {
8695
9013
  if (typeof window === "undefined") return;
8696
- await this.loadFacebookSdk();
8697
- if (window.FB) {
8698
- window.FB.init({ appId: this.appId, cookie: true, xfbml: true, version: this.version });
9014
+ try {
9015
+ await this.loadFacebookSdk();
9016
+ if (window.FB) {
9017
+ window.FB.init({ appId: this.appId, cookie: true, xfbml: true, version: this.version });
9018
+ }
9019
+ } catch (e) {
8699
9020
  }
8700
9021
  }
8701
- signIn() {
9022
+ buildAuthUrl() {
9023
+ const state = (0, _pkce.generateRandomState)();
9024
+ this.lastState = state;
9025
+ return (0, _pkce.buildAuthorizeUrl)({ authorizeUrl: `https://www.facebook.com/${this.version}/dialog/oauth`, clientId: this.appId, redirectUri: this.redirectUri || (typeof window !== "undefined" ? window.location.origin : ""), responseType: "token", scope: this.scope, state });
9026
+ }
9027
+ async signIn() {
8702
9028
  if (typeof window === "undefined") return;
8703
- if (window.FB && this.isPopup) {
8704
- window.FB.login((response) => {
8705
- if (response.authResponse) {
8706
- this.accessToken = response.authResponse.accessToken;
8707
- if (this.onSuccess) this.onSuccess(response.authResponse);
8708
- } else {
8709
- const err = new Error("Facebook login cancelled or not authorized");
8710
- if (this.onFail) this.onFail(err);
8711
- }
8712
- }, { scope: this.scope });
8713
- } else {
8714
- const redirectUri = this.redirectUri || (typeof window !== "undefined" ? window.location.origin : "");
8715
- const authUrl = (0, _pkce.buildAuthorizeUrl)({ authorizeUrl: `https://www.facebook.com/${this.version}/dialog/oauth`, clientId: this.appId, redirectUri, scope: this.scope, responseType: "token" });
8716
- if (this.isPopup) {
8717
- window.open(authUrl, "facebook_login", "width=600,height=700");
8718
- } else {
9029
+ if (window.FB && typeof window.FB.login === "function" && !this.redirectUri) {
9030
+ return new Promise((resolve, reject) => {
9031
+ window.FB.login((response) => {
9032
+ if (response.authResponse) {
9033
+ this.accessToken = response.authResponse.accessToken;
9034
+ this.fetchUserInfo().then((user) => {
9035
+ if (this.onSuccess) this.onSuccess(user || response);
9036
+ resolve();
9037
+ }).catch(() => {
9038
+ if (this.onSuccess) this.onSuccess(response);
9039
+ resolve();
9040
+ });
9041
+ } else {
9042
+ const err = new Error("Meta login cancelled or failed");
9043
+ if (this.onFail) this.onFail(err);
9044
+ reject(err);
9045
+ }
9046
+ }, { scope: this.scope });
9047
+ });
9048
+ }
9049
+ const authUrl = this.buildAuthUrl();
9050
+ if (this.isPopup) {
9051
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "meta_oauth_login", 600, 700);
9052
+ if (!popup) {
8719
9053
  window.location.href = authUrl;
9054
+ return;
8720
9055
  }
9056
+ try {
9057
+ const result = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: this.lastState });
9058
+ if (result.access_token || result.token) {
9059
+ this.accessToken = result.access_token || result.token || null;
9060
+ } else if (result.code) {
9061
+ this.accessToken = result.code;
9062
+ }
9063
+ if (this.onSuccess) this.onSuccess(result);
9064
+ } catch (err) {
9065
+ if (this.onFail) this.onFail(err);
9066
+ }
9067
+ } else {
9068
+ window.location.href = authUrl;
8721
9069
  }
8722
9070
  }
8723
9071
  signOut() {
8724
- if (typeof window !== "undefined" && window.FB) {
8725
- window.FB.logout(() => {
8726
- this.accessToken = null;
8727
- this.userInfo = null;
9072
+ this.accessToken = null;
9073
+ this.userInfo = null;
9074
+ if (typeof window !== "undefined" && window.FB && typeof window.FB.logout === "function") {
9075
+ try {
9076
+ window.FB.logout(() => {
9077
+ });
9078
+ } catch (e) {
9079
+ }
9080
+ }
9081
+ }
9082
+ async fetchUserInfo() {
9083
+ var _window_FB_getAccessToken, _window_FB;
9084
+ if (typeof window === "undefined") return null;
9085
+ if (window.FB && typeof window.FB.api === "function" && ((_window_FB_getAccessToken = (_window_FB = window.FB).getAccessToken) === null || _window_FB_getAccessToken === void 0 ? void 0 : _window_FB_getAccessToken.call(_window_FB))) {
9086
+ return new Promise((resolve, reject) => {
9087
+ window.FB.api("/me", { fields: "id,name,email,picture" }, (response) => {
9088
+ if (!response || response.error) {
9089
+ reject((response === null || response === void 0 ? void 0 : response.error) || new Error("Failed to fetch FB user"));
9090
+ } else {
9091
+ this.userInfo = response;
9092
+ resolve(response);
9093
+ }
9094
+ });
8728
9095
  });
8729
- } else {
8730
- this.accessToken = null;
8731
- this.userInfo = null;
8732
9096
  }
9097
+ if (this.accessToken) {
9098
+ const res = await fetch(`https://graph.facebook.com/${this.version}/me?fields=id,name,email,picture`, { headers: { Authorization: `Bearer ${this.accessToken}` } });
9099
+ if (res.ok) {
9100
+ this.userInfo = await res.json();
9101
+ return this.userInfo;
9102
+ }
9103
+ }
9104
+ return null;
8733
9105
  }
8734
9106
  async getAccessToken() {
8735
9107
  return this.accessToken;
8736
9108
  }
8737
9109
  async getUserInfo() {
8738
- if (typeof window === "undefined" || !window.FB || !this.accessToken) {
8739
- return this.userInfo;
9110
+ if (this.userInfo) return this.userInfo;
9111
+ try {
9112
+ return await this.fetchUserInfo();
9113
+ } catch (e) {
9114
+ return null;
8740
9115
  }
8741
- return new Promise((resolve) => {
8742
- window.FB.api("/me", { fields: "id,name,email,picture" }, (res) => {
8743
- this.userInfo = res;
8744
- resolve(res);
8745
- });
8746
- });
8747
9116
  }
8748
9117
  setToken(token) {
8749
9118
  this.accessToken = token;
@@ -8758,12 +9127,13 @@ var require_metaAuth = __commonJS({
8758
9127
  _define_property(this, "redirectUri", void 0);
8759
9128
  _define_property(this, "scope", void 0);
8760
9129
  _define_property(this, "isPopup", void 0);
9130
+ _define_property(this, "lastState", void 0);
8761
9131
  _define_property(this, "onSuccess", void 0);
8762
9132
  _define_property(this, "onFail", void 0);
8763
9133
  _define_property(this, "accessToken", null);
8764
9134
  _define_property(this, "userInfo", null);
8765
9135
  this.appId = options.appId;
8766
- this.version = options.version || "v19.0";
9136
+ this.version = options.version || "v21.0";
8767
9137
  this.redirectUri = options.redirectUri;
8768
9138
  this.scope = options.scope || "public_profile,email";
8769
9139
  var _options_isPopup;
@@ -8772,7 +9142,6 @@ var require_metaAuth = __commonJS({
8772
9142
  this.onFail = options.onFail;
8773
9143
  }
8774
9144
  };
8775
- var FacebookAuthProvider2 = MetaAuthProvider2;
8776
9145
  var _default = MetaAuthProvider2;
8777
9146
  }
8778
9147
  });
@@ -8791,6 +9160,7 @@ var require_kakaoAuth = __commonJS({
8791
9160
  return _default;
8792
9161
  } });
8793
9162
  var _isautil = require_dist();
9163
+ var _pkce = require_pkce();
8794
9164
  function _define_property(obj, key, value) {
8795
9165
  if (key in obj) {
8796
9166
  Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
@@ -8800,6 +9170,11 @@ var require_kakaoAuth = __commonJS({
8800
9170
  return obj;
8801
9171
  }
8802
9172
  var KakaoAuthProvider2 = class {
9173
+ buildAuthUrl() {
9174
+ const state = this.state || (0, _pkce.generateRandomState)();
9175
+ this.lastState = state;
9176
+ return (0, _pkce.buildAuthorizeUrl)({ authorizeUrl: "https://kauth.kakao.com/oauth/authorize", clientId: this.restApiKey || this.javascriptKey, redirectUri: this.redirectUri || (typeof window !== "undefined" ? window.location.origin : ""), responseType: "code", scope: this.scope, state });
9177
+ }
8803
9178
  async loadKakaoSdk() {
8804
9179
  if (typeof window !== "undefined" && window.Kakao) {
8805
9180
  return;
@@ -8808,54 +9183,158 @@ var require_kakaoAuth = __commonJS({
8808
9183
  }
8809
9184
  async init() {
8810
9185
  if (typeof window === "undefined") return;
8811
- await this.loadKakaoSdk();
8812
- if (window.Kakao && !window.Kakao.isInitialized()) {
8813
- window.Kakao.init(this.javascriptKey);
9186
+ try {
9187
+ await this.loadKakaoSdk();
9188
+ if (window.Kakao && !window.Kakao.isInitialized()) {
9189
+ window.Kakao.init(this.javascriptKey);
9190
+ }
9191
+ } catch (e) {
8814
9192
  }
8815
9193
  }
8816
- signIn() {
8817
- if (typeof window === "undefined" || !window.Kakao) {
8818
- console.error("[ISA-AUTH] Kakao SDK not loaded");
9194
+ async signIn() {
9195
+ var _window_Kakao_Auth;
9196
+ if (typeof window === "undefined") return;
9197
+ if (this.isPopup) {
9198
+ var _window_Kakao_Auth1;
9199
+ if (window.Kakao && typeof ((_window_Kakao_Auth1 = window.Kakao.Auth) === null || _window_Kakao_Auth1 === void 0 ? void 0 : _window_Kakao_Auth1.login) === "function" && !this.redirectUri) {
9200
+ return new Promise((resolve, reject) => {
9201
+ window.Kakao.Auth.login({ scope: this.scope, success: async (authObj) => {
9202
+ this.accessToken = authObj.access_token;
9203
+ try {
9204
+ await this.fetchUserInfo();
9205
+ } catch (e) {
9206
+ }
9207
+ if (this.onSuccess) this.onSuccess(authObj);
9208
+ resolve();
9209
+ }, fail: (err) => {
9210
+ if (this.onFail) this.onFail(err);
9211
+ reject(err);
9212
+ } });
9213
+ });
9214
+ }
9215
+ const authUrl = this.buildAuthUrl();
9216
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "kakao_login", 480, 600);
9217
+ if (!popup) {
9218
+ window.location.href = authUrl;
9219
+ return;
9220
+ }
9221
+ try {
9222
+ const result = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: this.lastState });
9223
+ if (result.access_token || result.token) {
9224
+ this.accessToken = result.access_token || result.token || null;
9225
+ } else if (result.code) {
9226
+ this.accessToken = result.code;
9227
+ }
9228
+ if (this.onSuccess) this.onSuccess(result);
9229
+ } catch (err) {
9230
+ if (this.onFail) this.onFail(err);
9231
+ }
8819
9232
  return;
8820
9233
  }
8821
- if (this.redirectUri) {
9234
+ if (window.Kakao && typeof ((_window_Kakao_Auth = window.Kakao.Auth) === null || _window_Kakao_Auth === void 0 ? void 0 : _window_Kakao_Auth.authorize) === "function" && this.redirectUri) {
8822
9235
  window.Kakao.Auth.authorize({ redirectUri: this.redirectUri, scope: this.scope });
8823
- } else {
8824
- window.Kakao.Auth.login({ scope: this.scope, success: (authObj) => {
8825
- if (this.onSuccess) this.onSuccess(authObj);
8826
- }, fail: (err) => {
8827
- if (this.onFail) this.onFail(err);
8828
- } });
9236
+ return;
8829
9237
  }
9238
+ window.location.href = this.buildAuthUrl();
8830
9239
  }
8831
9240
  signOut() {
8832
- if (typeof window !== "undefined" && window.Kakao && window.Kakao.Auth.getAccessToken()) {
8833
- window.Kakao.Auth.logout(() => {
8834
- console.log("[ISA-AUTH] Kakao logged out");
9241
+ var _window_Kakao_Auth_getAccessToken, _window_Kakao_Auth;
9242
+ this.accessToken = null;
9243
+ this.userInfo = null;
9244
+ if (typeof window !== "undefined" && window.Kakao && ((_window_Kakao_Auth = window.Kakao.Auth) === null || _window_Kakao_Auth === void 0 ? void 0 : (_window_Kakao_Auth_getAccessToken = _window_Kakao_Auth.getAccessToken) === null || _window_Kakao_Auth_getAccessToken === void 0 ? void 0 : _window_Kakao_Auth_getAccessToken.call(_window_Kakao_Auth))) {
9245
+ try {
9246
+ Promise.resolve(window.Kakao.Auth.logout(() => {
9247
+ })).catch(() => {
9248
+ });
9249
+ } catch (e) {
9250
+ }
9251
+ }
9252
+ }
9253
+ async fetchUserInfo() {
9254
+ var _window_Kakao_API, _window_Kakao_Auth_getAccessToken, _window_Kakao_Auth;
9255
+ if (typeof window === "undefined") return null;
9256
+ if (window.Kakao && typeof ((_window_Kakao_API = window.Kakao.API) === null || _window_Kakao_API === void 0 ? void 0 : _window_Kakao_API.request) === "function" && ((_window_Kakao_Auth = window.Kakao.Auth) === null || _window_Kakao_Auth === void 0 ? void 0 : (_window_Kakao_Auth_getAccessToken = _window_Kakao_Auth.getAccessToken) === null || _window_Kakao_Auth_getAccessToken === void 0 ? void 0 : _window_Kakao_Auth_getAccessToken.call(_window_Kakao_Auth))) {
9257
+ return new Promise((resolve, reject) => {
9258
+ let settled = false;
9259
+ const maybePromise = window.Kakao.API.request({ url: "/v2/user/me", success: (response) => {
9260
+ if (!settled) {
9261
+ settled = true;
9262
+ this.userInfo = response;
9263
+ resolve(response);
9264
+ }
9265
+ }, fail: (error) => {
9266
+ if (!settled) {
9267
+ settled = true;
9268
+ reject(error);
9269
+ }
9270
+ } });
9271
+ if (maybePromise && typeof maybePromise.then === "function") {
9272
+ maybePromise.then((response) => {
9273
+ if (!settled) {
9274
+ settled = true;
9275
+ this.userInfo = response;
9276
+ resolve(response);
9277
+ }
9278
+ }).catch((error) => {
9279
+ if (!settled) {
9280
+ settled = true;
9281
+ reject(error);
9282
+ }
9283
+ });
9284
+ }
8835
9285
  });
8836
9286
  }
9287
+ if (this.accessToken) {
9288
+ const res = await fetch("https://kapi.kakao.com/v2/user/me", { headers: { Authorization: `Bearer ${this.accessToken}`, "Content-Type": "application/x-www-form-urlencoded;charset=utf-8" } });
9289
+ if (res.ok) {
9290
+ this.userInfo = await res.json();
9291
+ return this.userInfo;
9292
+ }
9293
+ }
9294
+ return null;
8837
9295
  }
8838
9296
  async getAccessToken() {
8839
- if (typeof window !== "undefined" && window.Kakao) {
9297
+ var _window_Kakao_Auth, _window_Kakao;
9298
+ if (this.accessToken) return this.accessToken;
9299
+ if (typeof window !== "undefined" && ((_window_Kakao = window.Kakao) === null || _window_Kakao === void 0 ? void 0 : (_window_Kakao_Auth = _window_Kakao.Auth) === null || _window_Kakao_Auth === void 0 ? void 0 : _window_Kakao_Auth.getAccessToken)) {
8840
9300
  return window.Kakao.Auth.getAccessToken() || null;
8841
9301
  }
8842
9302
  return null;
8843
9303
  }
8844
9304
  async getUserInfo() {
8845
- if (typeof window === "undefined" || !window.Kakao) return null;
8846
- return new Promise((resolve, reject) => {
8847
- window.Kakao.API.request({ url: "/v2/user/me", success: (response) => resolve(response), fail: (error) => reject(error) });
8848
- });
9305
+ if (this.userInfo) return this.userInfo;
9306
+ try {
9307
+ return await this.fetchUserInfo();
9308
+ } catch (e) {
9309
+ return null;
9310
+ }
9311
+ }
9312
+ setToken(token) {
9313
+ this.accessToken = token;
9314
+ }
9315
+ setUser(user) {
9316
+ this.userInfo = user;
9317
+ if (this.onSuccess) this.onSuccess(user);
8849
9318
  }
8850
9319
  constructor(options) {
8851
9320
  _define_property(this, "javascriptKey", void 0);
9321
+ _define_property(this, "restApiKey", void 0);
8852
9322
  _define_property(this, "redirectUri", void 0);
8853
9323
  _define_property(this, "scope", void 0);
9324
+ _define_property(this, "isPopup", void 0);
9325
+ _define_property(this, "state", void 0);
9326
+ _define_property(this, "lastState", void 0);
8854
9327
  _define_property(this, "onSuccess", void 0);
8855
9328
  _define_property(this, "onFail", void 0);
9329
+ _define_property(this, "accessToken", null);
9330
+ _define_property(this, "userInfo", null);
8856
9331
  this.javascriptKey = options.javascriptKey;
9332
+ this.restApiKey = options.restApiKey;
8857
9333
  this.redirectUri = options.redirectUri;
8858
9334
  this.scope = options.scope;
9335
+ var _options_isPopup;
9336
+ this.isPopup = (_options_isPopup = options.isPopup) !== null && _options_isPopup !== void 0 ? _options_isPopup : true;
9337
+ this.state = options.state;
8859
9338
  this.onSuccess = options.onSuccess;
8860
9339
  this.onFail = options.onFail;
8861
9340
  }
@@ -8878,6 +9357,7 @@ var require_naverAuth = __commonJS({
8878
9357
  return _default;
8879
9358
  } });
8880
9359
  var _isautil = require_dist();
9360
+ var _pkce = require_pkce();
8881
9361
  function _define_property(obj, key, value) {
8882
9362
  if (key in obj) {
8883
9363
  Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
@@ -8887,82 +9367,137 @@ var require_naverAuth = __commonJS({
8887
9367
  return obj;
8888
9368
  }
8889
9369
  var NaverAuthProvider2 = class {
8890
- async loadNaverSdk() {
8891
- if (typeof window !== "undefined" && window.naver) {
8892
- return;
8893
- }
9370
+ buildAuthUrl() {
9371
+ const state = this.state || (0, _pkce.generateRandomState)();
9372
+ this.lastState = state;
9373
+ return (0, _pkce.buildAuthorizeUrl)({ authorizeUrl: "https://nid.naver.com/oauth2.0/authorize", clientId: this.clientId, redirectUri: this.callbackUrl, responseType: this.responseType, state });
9374
+ }
9375
+ async loadLegacySdk() {
9376
+ var _window_naver;
9377
+ if (typeof window === "undefined" || ((_window_naver = window.naver) === null || _window_naver === void 0 ? void 0 : _window_naver.LoginWithNaverId)) return;
8894
9378
  await (0, _isautil.loadCDN)("naver-login-sdk", "https://static.nid.naver.com/js/naveridlogin_js_sdk_2.0.2.js", { async: true, defer: true });
8895
9379
  }
8896
9380
  async init() {
8897
9381
  if (typeof window === "undefined") return;
8898
- await this.loadNaverSdk();
8899
- if (window.naver && window.naver.LoginWithNaverId) {
8900
- let btnEl = document.getElementById(this.loginButtonId);
8901
- if (!btnEl && typeof document !== "undefined") {
8902
- btnEl = document.createElement("div");
8903
- btnEl.id = this.loginButtonId;
8904
- btnEl.style.display = "none";
8905
- document.body.appendChild(btnEl);
8906
- }
8907
- this.naverLoginInstance = new window.naver.LoginWithNaverId({ clientId: this.clientId, callbackUrl: this.callbackUrl, isPopup: this.isPopup, loginButton: { color: "green", type: 3, height: 48 } });
8908
- try {
8909
- this.naverLoginInstance.init();
8910
- } catch (err) {
9382
+ if (this.useLegacySdk) {
9383
+ var _window_naver;
9384
+ await this.loadLegacySdk();
9385
+ if ((_window_naver = window.naver) === null || _window_naver === void 0 ? void 0 : _window_naver.LoginWithNaverId) {
9386
+ const btnId = this.loginButtonId || "naverIdLogin";
9387
+ let btnEl = document.getElementById(btnId);
9388
+ if (!btnEl && typeof document !== "undefined") {
9389
+ btnEl = document.createElement("div");
9390
+ btnEl.id = btnId;
9391
+ btnEl.style.display = "none";
9392
+ document.body.appendChild(btnEl);
9393
+ }
9394
+ this.naverLoginInstance = new window.naver.LoginWithNaverId({ clientId: this.clientId, callbackUrl: this.callbackUrl, isPopup: this.isPopup, loginButton: { color: "green", type: 3, height: 48 } });
9395
+ try {
9396
+ this.naverLoginInstance.init();
9397
+ } catch (err) {
9398
+ }
8911
9399
  }
8912
9400
  }
8913
9401
  }
8914
- signIn() {
9402
+ async signIn() {
8915
9403
  if (typeof window === "undefined") return;
8916
- const buttonElement = document.getElementById(this.loginButtonId);
8917
- const link = buttonElement === null || buttonElement === void 0 ? void 0 : buttonElement.querySelector("a");
8918
- if (link) {
8919
- link.click();
8920
- } else {
8921
- const authUrl = `https://nid.naver.com/oauth2.0/authorize?response_type=token&client_id=${this.clientId}&redirect_uri=${encodeURIComponent(this.callbackUrl)}&state=${Math.random().toString(36).substring(2, 15)}`;
8922
- if (this.isPopup) {
8923
- window.open(authUrl, "naver_login", "width=500,height=600");
8924
- } else {
9404
+ if (this.useLegacySdk && this.naverLoginInstance) {
9405
+ const btnId = this.loginButtonId || "naverIdLogin";
9406
+ const buttonElement = document.getElementById(btnId);
9407
+ const link = buttonElement === null || buttonElement === void 0 ? void 0 : buttonElement.querySelector("a");
9408
+ if (link) {
9409
+ link.click();
9410
+ return;
9411
+ }
9412
+ }
9413
+ const authUrl = this.buildAuthUrl();
9414
+ if (this.isPopup) {
9415
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "naver_oauth_login", 500, 650);
9416
+ if (!popup) {
8925
9417
  window.location.href = authUrl;
9418
+ return;
9419
+ }
9420
+ try {
9421
+ const result = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: this.lastState });
9422
+ if (result.access_token || result.token) {
9423
+ this.accessToken = result.access_token || result.token || null;
9424
+ } else if (result.code) {
9425
+ this.accessToken = result.code;
9426
+ }
9427
+ if (this.onSuccess) this.onSuccess(result);
9428
+ } catch (err) {
9429
+ if (this.onFail) this.onFail(err);
8926
9430
  }
9431
+ } else {
9432
+ window.location.href = authUrl;
8927
9433
  }
8928
9434
  }
8929
9435
  signOut() {
8930
- if (this.naverLoginInstance) {
8931
- this.naverLoginInstance.logout();
9436
+ this.accessToken = null;
9437
+ this.userInfo = null;
9438
+ if (this.naverLoginInstance && typeof this.naverLoginInstance.logout === "function") {
9439
+ try {
9440
+ this.naverLoginInstance.logout();
9441
+ } catch (e) {
9442
+ }
8932
9443
  }
8933
9444
  }
8934
9445
  async getAccessToken() {
9446
+ if (this.accessToken) return this.accessToken;
8935
9447
  if (this.naverLoginInstance && this.naverLoginInstance.accessToken) {
8936
9448
  return this.naverLoginInstance.accessToken.accessToken || null;
8937
9449
  }
8938
9450
  return null;
8939
9451
  }
8940
9452
  async getUserInfo() {
8941
- if (!this.naverLoginInstance) return null;
8942
- return new Promise((resolve) => {
8943
- this.naverLoginInstance.getLoginStatus((status) => {
8944
- if (status) {
8945
- resolve(this.naverLoginInstance.user);
8946
- } else {
8947
- resolve(null);
8948
- }
9453
+ if (this.userInfo) return this.userInfo;
9454
+ if (this.naverLoginInstance && typeof this.naverLoginInstance.getLoginStatus === "function") {
9455
+ return new Promise((resolve) => {
9456
+ this.naverLoginInstance.getLoginStatus((status) => {
9457
+ if (status) {
9458
+ resolve(this.naverLoginInstance.user);
9459
+ } else {
9460
+ resolve(null);
9461
+ }
9462
+ });
8949
9463
  });
8950
- });
9464
+ }
9465
+ return null;
9466
+ }
9467
+ setToken(token) {
9468
+ this.accessToken = token;
9469
+ }
9470
+ setUser(user) {
9471
+ this.userInfo = user;
9472
+ if (this.onSuccess) this.onSuccess(user);
8951
9473
  }
8952
9474
  constructor(options) {
8953
9475
  _define_property(this, "clientId", void 0);
8954
9476
  _define_property(this, "callbackUrl", void 0);
9477
+ _define_property(this, "responseType", void 0);
8955
9478
  _define_property(this, "isPopup", void 0);
9479
+ _define_property(this, "state", void 0);
9480
+ _define_property(this, "lastState", void 0);
9481
+ _define_property(this, "useLegacySdk", void 0);
8956
9482
  _define_property(this, "loginButtonId", void 0);
8957
9483
  _define_property(this, "callbackHandle", void 0);
8958
- _define_property(this, "naverLoginInstance", void 0);
9484
+ _define_property(this, "onSuccess", void 0);
9485
+ _define_property(this, "onFail", void 0);
9486
+ _define_property(this, "naverLoginInstance", null);
9487
+ _define_property(this, "accessToken", null);
9488
+ _define_property(this, "userInfo", null);
8959
9489
  this.clientId = options.clientId;
8960
9490
  this.callbackUrl = options.callbackUrl;
9491
+ this.responseType = options.responseType || "code";
8961
9492
  var _options_isPopup;
8962
9493
  this.isPopup = (_options_isPopup = options.isPopup) !== null && _options_isPopup !== void 0 ? _options_isPopup : true;
8963
- var _options_loginButtonId;
8964
- this.loginButtonId = (_options_loginButtonId = options.loginButtonId) !== null && _options_loginButtonId !== void 0 ? _options_loginButtonId : "naverIdLogin";
9494
+ this.state = options.state;
9495
+ var _options_useLegacySdk;
9496
+ this.useLegacySdk = (_options_useLegacySdk = options.useLegacySdk) !== null && _options_useLegacySdk !== void 0 ? _options_useLegacySdk : false;
9497
+ this.loginButtonId = options.loginButtonId;
8965
9498
  this.callbackHandle = options.callbackHandle;
9499
+ this.onSuccess = options.onSuccess;
9500
+ this.onFail = options.onFail;
8966
9501
  }
8967
9502
  };
8968
9503
  var _default = NaverAuthProvider2;
@@ -8995,8 +9530,9 @@ var require_lineAuth = __commonJS({
8995
9530
  async init() {
8996
9531
  }
8997
9532
  buildLoginUrl() {
8998
- const state = this.state || Math.random().toString(36).substring(2, 15);
8999
- const nonce = this.nonce || Math.random().toString(36).substring(2, 15);
9533
+ const state = this.state || (0, _pkce.generateRandomState)();
9534
+ this.lastState = state;
9535
+ const nonce = this.nonce || (0, _pkce.generateRandomState)();
9000
9536
  const extraParams = { nonce };
9001
9537
  if (this.botPrompt) extraParams.bot_prompt = this.botPrompt;
9002
9538
  return (0, _pkce.buildAuthorizeUrl)({ authorizeUrl: "https://access.line.me/oauth2/v2.1/authorize", clientId: this.clientId, redirectUri: this.callbackUrl, responseType: "code", scope: this.scope, state, extraParams });
@@ -9006,19 +9542,27 @@ var require_lineAuth = __commonJS({
9006
9542
  try {
9007
9543
  const authUrl = this.buildLoginUrl();
9008
9544
  if (this.isPopup) {
9009
- const width = 500;
9010
- const height = 650;
9011
- const left = window.screenX + (window.outerWidth - width) / 2;
9012
- const top = window.screenY + (window.outerHeight - height) / 2;
9013
- const popup = window.open(authUrl, "line_oauth_login", `width=${width},height=${height},left=${left},top=${top},scrollbars=yes`);
9545
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "line_oauth_login", 500, 650);
9014
9546
  if (!popup) {
9015
9547
  window.location.href = authUrl;
9548
+ return;
9549
+ }
9550
+ try {
9551
+ const result = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: this.lastState });
9552
+ if (result.access_token || result.token) {
9553
+ this.accessToken = result.access_token || result.token || null;
9554
+ } else if (result.code) {
9555
+ this.accessToken = result.code;
9556
+ }
9557
+ if (this.onSuccess) this.onSuccess(result);
9558
+ } catch (err) {
9559
+ if (this.onFail) this.onFail(err);
9016
9560
  }
9017
9561
  } else {
9018
9562
  window.location.href = authUrl;
9019
9563
  }
9020
9564
  } catch (err) {
9021
- console.error("[ISA-AUTH] LINE sign-in failed:", err);
9565
+ console.error("[ISA-AUTH] Line sign-in failed:", err);
9022
9566
  if (this.onFail) this.onFail(err);
9023
9567
  }
9024
9568
  }
@@ -9044,6 +9588,7 @@ var require_lineAuth = __commonJS({
9044
9588
  _define_property(this, "callbackUrl", void 0);
9045
9589
  _define_property(this, "scope", void 0);
9046
9590
  _define_property(this, "state", void 0);
9591
+ _define_property(this, "lastState", void 0);
9047
9592
  _define_property(this, "nonce", void 0);
9048
9593
  _define_property(this, "isPopup", void 0);
9049
9594
  _define_property(this, "botPrompt", void 0);
@@ -9184,7 +9729,8 @@ var require_githubAuth = __commonJS({
9184
9729
  }
9185
9730
  }
9186
9731
  async buildLoginUrl() {
9187
- const state = this.state || Math.random().toString(36).substring(2, 15);
9732
+ const state = this.state || (0, _pkce.generateRandomState)();
9733
+ this.lastState = state;
9188
9734
  let codeChallenge;
9189
9735
  if (this.usePkce && this.codeVerifier) {
9190
9736
  codeChallenge = await (0, _pkce.generateCodeChallenge)(this.codeVerifier);
@@ -9196,13 +9742,21 @@ var require_githubAuth = __commonJS({
9196
9742
  try {
9197
9743
  const authUrl = await this.buildLoginUrl();
9198
9744
  if (this.isPopup) {
9199
- const width = 600;
9200
- const height = 700;
9201
- const left = window.screenX + (window.outerWidth - width) / 2;
9202
- const top = window.screenY + (window.outerHeight - height) / 2;
9203
- const popup = window.open(authUrl, "github_oauth_login", `width=${width},height=${height},left=${left},top=${top},scrollbars=yes`);
9745
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "github_oauth_login", 600, 700);
9204
9746
  if (!popup) {
9205
9747
  window.location.href = authUrl;
9748
+ return;
9749
+ }
9750
+ try {
9751
+ const result = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: this.lastState });
9752
+ if (result.access_token || result.token) {
9753
+ this.accessToken = result.access_token || result.token || null;
9754
+ } else if (result.code) {
9755
+ this.accessToken = result.code;
9756
+ }
9757
+ if (this.onSuccess) this.onSuccess(result);
9758
+ } catch (err) {
9759
+ if (this.onFail) this.onFail(err);
9206
9760
  }
9207
9761
  } else {
9208
9762
  window.location.href = authUrl;
@@ -9238,6 +9792,7 @@ var require_githubAuth = __commonJS({
9238
9792
  _define_property(this, "scope", void 0);
9239
9793
  _define_property(this, "isPopup", void 0);
9240
9794
  _define_property(this, "state", void 0);
9795
+ _define_property(this, "lastState", void 0);
9241
9796
  _define_property(this, "usePkce", void 0);
9242
9797
  _define_property(this, "onSuccess", void 0);
9243
9798
  _define_property(this, "onFail", void 0);
@@ -9292,7 +9847,8 @@ var require_discordAuth = __commonJS({
9292
9847
  }
9293
9848
  }
9294
9849
  async buildLoginUrl() {
9295
- const state = this.state || Math.random().toString(36).substring(2, 15);
9850
+ const state = this.state || (0, _pkce.generateRandomState)();
9851
+ this.lastState = state;
9296
9852
  let codeChallenge;
9297
9853
  if (this.usePkce && this.codeVerifier) {
9298
9854
  codeChallenge = await (0, _pkce.generateCodeChallenge)(this.codeVerifier);
@@ -9306,13 +9862,21 @@ var require_discordAuth = __commonJS({
9306
9862
  try {
9307
9863
  const authUrl = await this.buildLoginUrl();
9308
9864
  if (this.isPopup) {
9309
- const width = 580;
9310
- const height = 750;
9311
- const left = window.screenX + (window.outerWidth - width) / 2;
9312
- const top = window.screenY + (window.outerHeight - height) / 2;
9313
- const popup = window.open(authUrl, "discord_oauth_login", `width=${width},height=${height},left=${left},top=${top},scrollbars=yes`);
9865
+ const popup = (0, _pkce.openOAuthPopup)(authUrl, "discord_oauth_login", 500, 750);
9314
9866
  if (!popup) {
9315
9867
  window.location.href = authUrl;
9868
+ return;
9869
+ }
9870
+ try {
9871
+ const result = await (0, _pkce.waitForOAuthPopupMessage)(popup, { expectedState: this.lastState });
9872
+ if (result.access_token || result.token) {
9873
+ this.accessToken = result.access_token || result.token || null;
9874
+ } else if (result.code) {
9875
+ this.accessToken = result.code;
9876
+ }
9877
+ if (this.onSuccess) this.onSuccess(result);
9878
+ } catch (err) {
9879
+ if (this.onFail) this.onFail(err);
9316
9880
  }
9317
9881
  } else {
9318
9882
  window.location.href = authUrl;
@@ -9348,6 +9912,7 @@ var require_discordAuth = __commonJS({
9348
9912
  _define_property(this, "scope", void 0);
9349
9913
  _define_property(this, "isPopup", void 0);
9350
9914
  _define_property(this, "state", void 0);
9915
+ _define_property(this, "lastState", void 0);
9351
9916
  _define_property(this, "usePkce", void 0);
9352
9917
  _define_property(this, "prompt", void 0);
9353
9918
  _define_property(this, "onSuccess", void 0);
@@ -9421,6 +9986,7 @@ var require_web3Auth = __commonJS({
9421
9986
  }, get formatSiweMessage() {
9422
9987
  return formatSiweMessage;
9423
9988
  } });
9989
+ var _pkce = require_pkce();
9424
9990
  function _define_property(obj, key, value) {
9425
9991
  if (key in obj) {
9426
9992
  Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
@@ -9471,7 +10037,7 @@ Expiration Time: ${params.expirationTime}`;
9471
10037
  const accounts = await window.ethereum.request({ method: "eth_requestAccounts" });
9472
10038
  const address = accounts[0];
9473
10039
  this.connectedAddress = address;
9474
- const nonce = this.nonceGenerator ? await this.nonceGenerator() : Math.random().toString(36).substring(2, 12);
10040
+ const nonce = this.nonceGenerator ? await this.nonceGenerator() : (0, _pkce.generateRandomState)(16);
9475
10041
  const message = formatSiweMessage({ domain: this.domain, address, statement: this.statement, uri: window.location.origin, chainId: this.chainId, nonce });
9476
10042
  const signature = await window.ethereum.request({ method: "personal_sign", params: [message, address] });
9477
10043
  this.lastSignature = signature;
@@ -9492,6 +10058,7 @@ Expiration Time: ${params.expirationTime}`;
9492
10058
  return this.lastSignature;
9493
10059
  }
9494
10060
  async getUserInfo() {
10061
+ if (!this.connectedAddress) return null;
9495
10062
  return { address: this.connectedAddress, chainId: this.chainId };
9496
10063
  }
9497
10064
  constructor(options = {}) {
@@ -9607,7 +10174,7 @@ var require_worldAuth = __commonJS({
9607
10174
  if (typeof window === "undefined") {
9608
10175
  throw new Error("World ID authentication requires a browser environment.");
9609
10176
  }
9610
- return { merkle_root: "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", nullifier_hash: "0xworld_orb_" + Math.random().toString(36).substring(2, 10), proof: "0xzkproof_orb_verified_signature", verification_level: this.verificationLevel, credential_type: this.verificationLevel, action: this.action, signal: this.signal };
10177
+ throw new Error("[ISA-AUTH] World ID IDKit widget (window.IDKit) is not loaded. Load @worldcoin/idkit or enable mockProof: true for local testing.");
9611
10178
  }
9612
10179
  signOut() {
9613
10180
  this.lastProof = null;
@@ -9664,6 +10231,7 @@ var require_passkeyAuth = __commonJS({
9664
10231
  }, get default() {
9665
10232
  return _default;
9666
10233
  } });
10234
+ var _pkce = require_pkce();
9667
10235
  function _define_property(obj, key, value) {
9668
10236
  if (key in obj) {
9669
10237
  Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true });
@@ -9711,8 +10279,12 @@ var require_passkeyAuth = __commonJS({
9711
10279
  }
9712
10280
  const challenge = options.challenge || crypto.getRandomValues(new Uint8Array(32));
9713
10281
  const userName = options.userName || this.userName || "user@example.com";
9714
- const userId = options.userId || this.userId || "user_" + Math.random().toString(36).substring(2, 9);
9715
- const credential = await navigator.credentials.create({ publicKey: { challenge, rp: { name: this.rpName, id: this.rpId }, user: { id: new TextEncoder().encode(userId), name: userName, displayName: this.userDisplayName || userName }, pubKeyCredParams: [{ alg: -7, type: "public-key" }, { alg: -257, type: "public-key" }], authenticatorSelection: { authenticatorAttachment: "platform", userVerification: "preferred", residentKey: "preferred" }, timeout: 6e4, attestation: "none" } });
10282
+ const userId = options.userId || this.userId || "user_" + (0, _pkce.generateRandomState)(12);
10283
+ const authenticatorSelection = { userVerification: "preferred", residentKey: "preferred" };
10284
+ if (options.authenticatorAttachment) {
10285
+ authenticatorSelection.authenticatorAttachment = options.authenticatorAttachment;
10286
+ }
10287
+ const credential = await navigator.credentials.create({ publicKey: { challenge, rp: { name: this.rpName, id: this.rpId }, user: { id: new TextEncoder().encode(userId), name: userName, displayName: this.userDisplayName || userName }, pubKeyCredParams: [{ alg: -7, type: "public-key" }, { alg: -257, type: "public-key" }], authenticatorSelection, timeout: 6e4, attestation: "none" } });
9716
10288
  return credential;
9717
10289
  }
9718
10290
  async signIn() {
@@ -10426,9 +10998,9 @@ var require_mappings = __commonJS({
10426
10998
  _export(exports, { get providerToButtonMap() {
10427
10999
  return providerToButtonMap2;
10428
11000
  }, get providerToServiceMap() {
10429
- return providerToServiceMap;
11001
+ return providerToServiceMap2;
10430
11002
  } });
10431
- var providerToServiceMap = { base: "base", jwt: "jwt", google: "jwt", kakao: "jwt", naver: "jwt", web3: "base", github: "jwt", apple: "jwt", discord: "jwt", line: "jwt", meta: "jwt", facebook: "jwt", world: "base", passkey: "base" };
11003
+ var providerToServiceMap2 = { base: "base", jwt: "jwt", google: "jwt", kakao: "jwt", naver: "jwt", web3: "base", github: "jwt", apple: "jwt", discord: "jwt", line: "jwt", meta: "jwt", facebook: "jwt", world: "base", passkey: "base" };
10432
11004
  var providerToButtonMap2 = { base: "none", jwt: "none", google: "google", kakao: "kakao", naver: "naver", web3: "web3", github: "github", apple: "apple", discord: "discord", line: "line", meta: "meta", facebook: "facebook", world: "world", passkey: "passkey" };
10433
11005
  }
10434
11006
  });
@@ -10614,6 +11186,7 @@ var require_http = __commonJS({
10614
11186
  refreshQueue = [];
10615
11187
  };
10616
11188
  const reqInterceptorId = axiosInstance.interceptors.request.use(async (config) => {
11189
+ config.headers = config.headers || {};
10617
11190
  const token = authClient.getAccessToken();
10618
11191
  if (token && !config.headers[headerName]) {
10619
11192
  config.headers[headerName] = `${tokenPrefix}${token}`;
@@ -10650,6 +11223,7 @@ var require_http = __commonJS({
10650
11223
  return new Promise((resolve, reject) => {
10651
11224
  refreshQueue.push((token) => {
10652
11225
  if (token) {
11226
+ originalRequest.headers = originalRequest.headers || {};
10653
11227
  originalRequest.headers[headerName] = `${tokenPrefix}${token}`;
10654
11228
  resolve(axiosInstance(originalRequest));
10655
11229
  } else {
@@ -10664,6 +11238,7 @@ var require_http = __commonJS({
10664
11238
  isRefreshing = false;
10665
11239
  processQueue(newToken);
10666
11240
  if (newToken) {
11241
+ originalRequest.headers = originalRequest.headers || {};
10667
11242
  originalRequest.headers[headerName] = `${tokenPrefix}${newToken}`;
10668
11243
  return axiosInstance(originalRequest);
10669
11244
  } else {
@@ -10794,14 +11369,14 @@ var require_authCore = __commonJS({
10794
11369
  getWebComponentAPI() {
10795
11370
  return { addAuthEventListener: _customAuth.addAuthEventListener, removeAuthEventListener: _customAuth.removeAuthEventListener, dispatchAuthEvent: _customAuth.dispatchAuthEvent, watchAuthState: this.serviceInstance.watchAuthState.bind(this.serviceInstance) };
10796
11371
  }
10797
- constructor({ providerType = "base", secret = "isa-auth-secret", storage = "memory", storagePrefix = "" } = {}) {
11372
+ constructor({ providerType = "base", serviceType: explicitServiceType, secret = "isa-auth-secret", storage = "memory", storagePrefix = "" } = {}) {
10798
11373
  _define_property(this, "provider", void 0);
10799
11374
  _define_property(this, "button", void 0);
10800
11375
  _define_property(this, "serviceInstance", void 0);
10801
11376
  _define_property(this, "lifecycle", void 0);
10802
11377
  (0, _store.configureStorage)({ type: storage, prefix: storagePrefix });
10803
- var _providerToServiceMap_providerType;
10804
- const serviceType = (_providerToServiceMap_providerType = _mappings.providerToServiceMap[providerType]) !== null && _providerToServiceMap_providerType !== void 0 ? _providerToServiceMap_providerType : "base";
11378
+ var _ref;
11379
+ const serviceType = (_ref = explicitServiceType !== null && explicitServiceType !== void 0 ? explicitServiceType : _mappings.providerToServiceMap[providerType]) !== null && _ref !== void 0 ? _ref : "base";
10805
11380
  var _providerToButtonMap_providerType;
10806
11381
  const buttonType = (_providerToButtonMap_providerType = _mappings.providerToButtonMap[providerType]) !== null && _providerToButtonMap_providerType !== void 0 ? _providerToButtonMap_providerType : "none";
10807
11382
  const isProviderRequired = providerType !== "base" && providerType !== "jwt";
@@ -11120,6 +11695,7 @@ function registerWebComponent() {
11120
11695
  this.dispatchEvent(new CustomEvent("isa-auth-click", { bubbles: true }));
11121
11696
  }
11122
11697
  });
11698
+ this.innerHTML = "";
11123
11699
  this.appendChild(button);
11124
11700
  }
11125
11701
  }
@@ -11127,17 +11703,23 @@ function registerWebComponent() {
11127
11703
  }
11128
11704
 
11129
11705
  // src/render/react.ts
11130
- function createReactAuthButton(authInstance) {
11706
+ function createReactAuthButton(authInstance, reactInstance) {
11131
11707
  return function IsaAuthButton(props) {
11132
11708
  var _a;
11133
11709
  const { provider, label, className, style, onSuccess, onError, children } = props;
11134
11710
  const targetAuth = props.auth || authInstance;
11711
+ if (!targetAuth) {
11712
+ console.warn("[ISA-AUTH] IsaAuth instance is required for IsaAuthButton.");
11713
+ return null;
11714
+ }
11135
11715
  if (typeof window === "undefined") {
11136
11716
  return null;
11137
11717
  }
11138
- const React = window.React;
11718
+ const React = reactInstance || globalThis.React || (typeof window !== "undefined" ? window.React : void 0);
11139
11719
  if (!React) {
11140
- console.warn("[ISA-AUTH] React is required to render IsaAuthButton.");
11720
+ console.warn(
11721
+ "[ISA-AUTH] React is required to render IsaAuthButton. Pass your React instance as createReactAuthButton(auth, React)."
11722
+ );
11141
11723
  return null;
11142
11724
  }
11143
11725
  const handleClick = async () => {
@@ -11175,8 +11757,48 @@ function createReactAuthButton(authInstance) {
11175
11757
  "data-provider": provider
11176
11758
  },
11177
11759
  iconElement,
11178
- React.createElement("span", null, children || label || data.text || `Sign in with ${provider}`)
11760
+ React.createElement(
11761
+ "span",
11762
+ null,
11763
+ children || label || data.text || `Sign in with ${provider}`
11764
+ )
11765
+ );
11766
+ };
11767
+ }
11768
+ function useIsaAuth(auth, reactInstance) {
11769
+ const React = reactInstance || globalThis.React || (typeof window !== "undefined" ? window.React : void 0);
11770
+ if (!React) {
11771
+ throw new Error(
11772
+ "[ISA-AUTH] React instance must be provided or globally available to use useIsaAuth(). Pass React as useIsaAuth(auth, React)."
11179
11773
  );
11774
+ }
11775
+ const service = auth.core.getService();
11776
+ const [isAuthenticated, setIsAuthenticated] = React.useState(auth.isAuthenticated());
11777
+ const [user, setUser] = React.useState(service.getAuthInfo());
11778
+ const [token, setToken] = React.useState(auth.getAccessToken());
11779
+ React.useEffect(() => {
11780
+ const syncState = () => {
11781
+ setIsAuthenticated(auth.isAuthenticated());
11782
+ setUser(service.getAuthInfo());
11783
+ setToken(auth.getAccessToken());
11784
+ };
11785
+ const unsubState = auth.subscribe(syncState);
11786
+ const unsubInfo = service.watchAuthInfo(syncState);
11787
+ return () => {
11788
+ if (typeof unsubState === "function") unsubState();
11789
+ if (typeof unsubInfo === "function") unsubInfo();
11790
+ };
11791
+ }, [auth, service]);
11792
+ return {
11793
+ isAuthenticated,
11794
+ user,
11795
+ token,
11796
+ signIn: (provider) => auth.signIn(provider),
11797
+ signOut: () => auth.signOut(),
11798
+ getAccessToken: () => auth.getAccessToken(),
11799
+ getUserInfo: () => auth.getUserInfo(),
11800
+ can: (permission, matchAll = true) => auth.can(permission, matchAll),
11801
+ is: (role, matchAll = false) => auth.is(role, matchAll)
11180
11802
  };
11181
11803
  }
11182
11804
 
@@ -11201,8 +11823,16 @@ var IsaAuth = class {
11201
11823
  const firstMultiKey = multiProviders ? Object.keys(multiProviders)[0] : void 0;
11202
11824
  const primaryProvider = provider || firstMultiKey || "base";
11203
11825
  this.providerType = primaryProvider;
11826
+ const allConfiguredProviders = /* @__PURE__ */ new Set([
11827
+ primaryProvider,
11828
+ ...multiProviders ? Object.keys(multiProviders) : []
11829
+ ]);
11830
+ const hasJwtProvider = Array.from(allConfiguredProviders).some(
11831
+ (p) => import_isa_auth_core.providerToServiceMap[p] === "jwt"
11832
+ );
11204
11833
  this.core = new import_isa_auth_core.default({
11205
11834
  providerType: primaryProvider,
11835
+ serviceType: hasJwtProvider ? "jwt" : void 0,
11206
11836
  secret,
11207
11837
  storage,
11208
11838
  storagePrefix
@@ -11247,6 +11877,8 @@ var IsaAuth = class {
11247
11877
  if (type === "google") {
11248
11878
  return new ProviderClass({
11249
11879
  clientId,
11880
+ redirectUri: callbackUrl,
11881
+ scope,
11250
11882
  callback: (token) => {
11251
11883
  this.setAccessToken(token);
11252
11884
  return token;
@@ -11387,8 +12019,26 @@ var IsaAuth = class {
11387
12019
  }
11388
12020
  signOut() {
11389
12021
  this.stopAutoRefresh();
12022
+ for (const instance of this.providerInstances.values()) {
12023
+ if (instance && instance !== this.providerInstance && typeof instance.signOut === "function") {
12024
+ try {
12025
+ instance.signOut();
12026
+ } catch {
12027
+ }
12028
+ }
12029
+ }
11390
12030
  this.authProvider.signOut();
11391
12031
  }
12032
+ /**
12033
+ * Handles incoming OAuth redirect or popup callbacks.
12034
+ * If running inside an authentication popup, communicates payload back to opener and closes popup.
12035
+ */
12036
+ static handleCallback(options) {
12037
+ return (0, import_isa_auth_core.handleOAuthCallback)(options);
12038
+ }
12039
+ handleCallback(options) {
12040
+ return (0, import_isa_auth_core.handleOAuthCallback)(options);
12041
+ }
11392
12042
  async refresh(refreshFn) {
11393
12043
  return await this.authProvider.refresh(refreshFn);
11394
12044
  }
@@ -11551,6 +12201,9 @@ var IsaAuth = class {
11551
12201
  subscribe(callback) {
11552
12202
  return this.authProvider.watchAuthState(callback);
11553
12203
  }
12204
+ watchAuthState(callback) {
12205
+ return this.subscribe(callback);
12206
+ }
11554
12207
  };
11555
12208
  var index_default = IsaAuth;
11556
12209
  var export_AppleAuthProvider = import_isa_auth_core.AppleAuthProvider;
@@ -11589,6 +12242,7 @@ var export_developerProviders = import_isa_auth_core.developerProviders;
11589
12242
  var export_evaluateGuard = import_isa_auth_core.evaluateGuard;
11590
12243
  var export_generateCodeChallenge = import_isa_auth_core.generateCodeChallenge;
11591
12244
  var export_generateCodeVerifier = import_isa_auth_core.generateCodeVerifier;
12245
+ var export_generateRandomState = import_isa_auth_core.generateRandomState;
11592
12246
  var export_getAccessToken = import_isa_auth_core.getAccessToken;
11593
12247
  var export_getAuthInfo = import_isa_auth_core.getAuthInfo;
11594
12248
  var export_getAuthState = import_isa_auth_core.getAuthState;
@@ -11599,15 +12253,18 @@ var export_getStorageAdapter = import_isa_auth_core.getStorageAdapter;
11599
12253
  var export_getTokenFromHeader = import_isa_auth_core.getTokenFromHeader;
11600
12254
  var export_getTokenPayload = import_isa_auth_core.getTokenPayload;
11601
12255
  var export_getTokenTimeRemaining = import_isa_auth_core.getTokenTimeRemaining;
12256
+ var export_handleOAuthCallback = import_isa_auth_core.handleOAuthCallback;
11602
12257
  var export_identityProviders = import_isa_auth_core.identityProviders;
11603
12258
  var export_isExpiredToken = import_isa_auth_core.isExpiredToken;
11604
12259
  var export_matchPermissionRule = import_isa_auth_core.matchPermissionRule;
12260
+ var export_openOAuthPopup = import_isa_auth_core.openOAuthPopup;
11605
12261
  var export_parseOAuthCallback = import_isa_auth_core.parseOAuthCallback;
11606
12262
  var export_resolveInheritedRoles = import_isa_auth_core.resolveInheritedRoles;
11607
12263
  var export_setRefreshToken = import_isa_auth_core.setRefreshToken;
11608
12264
  var export_socialProviders = import_isa_auth_core.socialProviders;
11609
12265
  var export_subscribeSelector = import_isa_auth_core.subscribeSelector;
11610
12266
  var export_updateAuthInfo = import_isa_auth_core.updateAuthInfo;
12267
+ var export_waitForOAuthPopupMessage = import_isa_auth_core.waitForOAuthPopupMessage;
11611
12268
  var export_web3Providers = import_isa_auth_core.web3Providers;
11612
12269
  export {
11613
12270
  export_AppleAuthProvider as AppleAuthProvider,
@@ -11650,6 +12307,7 @@ export {
11650
12307
  export_evaluateGuard as evaluateGuard,
11651
12308
  export_generateCodeChallenge as generateCodeChallenge,
11652
12309
  export_generateCodeVerifier as generateCodeVerifier,
12310
+ export_generateRandomState as generateRandomState,
11653
12311
  export_getAccessToken as getAccessToken,
11654
12312
  export_getAuthInfo as getAuthInfo,
11655
12313
  export_getAuthState as getAuthState,
@@ -11660,9 +12318,11 @@ export {
11660
12318
  export_getTokenFromHeader as getTokenFromHeader,
11661
12319
  export_getTokenPayload as getTokenPayload,
11662
12320
  export_getTokenTimeRemaining as getTokenTimeRemaining,
12321
+ export_handleOAuthCallback as handleOAuthCallback,
11663
12322
  export_identityProviders as identityProviders,
11664
12323
  export_isExpiredToken as isExpiredToken,
11665
12324
  export_matchPermissionRule as matchPermissionRule,
12325
+ export_openOAuthPopup as openOAuthPopup,
11666
12326
  export_parseOAuthCallback as parseOAuthCallback,
11667
12327
  render_exports as renderers,
11668
12328
  export_resolveInheritedRoles as resolveInheritedRoles,
@@ -11670,6 +12330,8 @@ export {
11670
12330
  export_socialProviders as socialProviders,
11671
12331
  export_subscribeSelector as subscribeSelector,
11672
12332
  export_updateAuthInfo as updateAuthInfo,
12333
+ useIsaAuth,
12334
+ export_waitForOAuthPopupMessage as waitForOAuthPopupMessage,
11673
12335
  export_web3Providers as web3Providers
11674
12336
  };
11675
12337
  /*! Bundled license information: