@blinkdotnew/sdk 0.14.1 → 0.14.2

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.d.mts CHANGED
@@ -733,6 +733,7 @@ declare class HttpClient {
733
733
  /**
734
734
  * Blink Auth Module - Client-side authentication management
735
735
  * Handles token storage, user state, and authentication flows
736
+ * Includes iframe token relay support for seamless preview authentication
736
737
  */
737
738
 
738
739
  type AuthStateChangeCallback = (state: AuthState) => void;
@@ -741,7 +742,18 @@ declare class BlinkAuth {
741
742
  private authState;
742
743
  private listeners;
743
744
  private readonly authUrl;
745
+ private parentWindowTokens;
746
+ private isIframe;
747
+ private tokenRequestSent;
744
748
  constructor(config: BlinkClientConfig);
749
+ /**
750
+ * Setup listener for tokens from parent window (iframe only)
751
+ */
752
+ private setupParentWindowListener;
753
+ /**
754
+ * Request tokens from parent window
755
+ */
756
+ private requestTokensFromParent;
745
757
  /**
746
758
  * Initialize authentication from stored tokens or URL fragments
747
759
  */
package/dist/index.d.ts CHANGED
@@ -733,6 +733,7 @@ declare class HttpClient {
733
733
  /**
734
734
  * Blink Auth Module - Client-side authentication management
735
735
  * Handles token storage, user state, and authentication flows
736
+ * Includes iframe token relay support for seamless preview authentication
736
737
  */
737
738
 
738
739
  type AuthStateChangeCallback = (state: AuthState) => void;
@@ -741,7 +742,18 @@ declare class BlinkAuth {
741
742
  private authState;
742
743
  private listeners;
743
744
  private readonly authUrl;
745
+ private parentWindowTokens;
746
+ private isIframe;
747
+ private tokenRequestSent;
744
748
  constructor(config: BlinkClientConfig);
749
+ /**
750
+ * Setup listener for tokens from parent window (iframe only)
751
+ */
752
+ private setupParentWindowListener;
753
+ /**
754
+ * Request tokens from parent window
755
+ */
756
+ private requestTokensFromParent;
745
757
  /**
746
758
  * Initialize authentication from stored tokens or URL fragments
747
759
  */
package/dist/index.js CHANGED
@@ -867,6 +867,9 @@ var BlinkAuth = class {
867
867
  authState;
868
868
  listeners = /* @__PURE__ */ new Set();
869
869
  authUrl = "https://blink.new";
870
+ parentWindowTokens = null;
871
+ isIframe = false;
872
+ tokenRequestSent = false;
870
873
  constructor(config) {
871
874
  this.config = config;
872
875
  this.authState = {
@@ -876,9 +879,80 @@ var BlinkAuth = class {
876
879
  isLoading: false
877
880
  };
878
881
  if (typeof window !== "undefined") {
882
+ this.isIframe = window.self !== window.top;
883
+ if (this.isIframe) {
884
+ console.log("\u{1F5BC}\uFE0F Detected iframe environment, setting up parent window communication");
885
+ this.setupParentWindowListener();
886
+ }
879
887
  this.initialize();
880
888
  }
881
889
  }
890
+ /**
891
+ * Setup listener for tokens from parent window (iframe only)
892
+ */
893
+ setupParentWindowListener() {
894
+ if (!this.isIframe) return;
895
+ window.addEventListener("message", (event) => {
896
+ const trustedOrigins = [
897
+ "https://blink.new",
898
+ "http://localhost:3000",
899
+ "http://localhost:3001",
900
+ "https://localhost:3000",
901
+ "https://localhost:3001"
902
+ ];
903
+ if (!trustedOrigins.includes(event.origin)) {
904
+ return;
905
+ }
906
+ if (event.data?.type === "BLINK_AUTH_TOKENS") {
907
+ console.log("\u{1F4E5} Received auth tokens from parent window", {
908
+ hasTokens: !!event.data.tokens,
909
+ projectId: event.data.projectId
910
+ });
911
+ const { tokens, projectId } = event.data;
912
+ if (projectId && projectId !== this.config.projectId) {
913
+ console.log("\u26A0\uFE0F Ignoring tokens for different project:", projectId);
914
+ return;
915
+ }
916
+ if (tokens) {
917
+ this.parentWindowTokens = tokens;
918
+ this.setTokens(tokens, false).then(() => {
919
+ console.log("\u2705 Tokens from parent window applied successfully");
920
+ }).catch((error) => {
921
+ console.error("\u274C Failed to apply parent window tokens:", error);
922
+ });
923
+ }
924
+ }
925
+ if (event.data?.type === "BLINK_AUTH_LOGOUT") {
926
+ console.log("\u{1F4E4} Received logout command from parent window");
927
+ this.clearTokens();
928
+ }
929
+ if (event.data?.type === "BLINK_AUTH_REFRESH") {
930
+ console.log("\u{1F504} Received token refresh from parent window");
931
+ const { tokens } = event.data;
932
+ if (tokens) {
933
+ this.parentWindowTokens = tokens;
934
+ this.setTokens(tokens, false).catch((error) => {
935
+ console.error("\u274C Failed to apply refreshed tokens:", error);
936
+ });
937
+ }
938
+ }
939
+ });
940
+ this.requestTokensFromParent();
941
+ }
942
+ /**
943
+ * Request tokens from parent window
944
+ */
945
+ requestTokensFromParent() {
946
+ if (!this.isIframe || this.tokenRequestSent) return;
947
+ if (window.parent !== window) {
948
+ console.log("\u{1F504} Requesting auth tokens from parent window");
949
+ window.parent.postMessage({
950
+ type: "BLINK_REQUEST_AUTH_TOKENS",
951
+ projectId: this.config.projectId
952
+ }, "*");
953
+ this.tokenRequestSent = true;
954
+ }
955
+ }
882
956
  /**
883
957
  * Initialize authentication from stored tokens or URL fragments
884
958
  */
@@ -886,6 +960,18 @@ var BlinkAuth = class {
886
960
  console.log("\u{1F680} Initializing Blink Auth...");
887
961
  this.setLoading(true);
888
962
  try {
963
+ if (this.isIframe) {
964
+ console.log("\u{1F50D} Detected iframe environment, waiting for parent tokens...");
965
+ for (let i = 0; i < 10; i++) {
966
+ if (this.parentWindowTokens) {
967
+ console.log("\u2705 Using tokens from parent window");
968
+ await this.setTokens(this.parentWindowTokens, false);
969
+ return;
970
+ }
971
+ await new Promise((resolve) => setTimeout(resolve, 100));
972
+ }
973
+ console.log("\u23F0 Timeout waiting for parent tokens, continuing with normal flow...");
974
+ }
889
975
  const tokensFromUrl = this.extractTokensFromUrl();
890
976
  if (tokensFromUrl) {
891
977
  console.log("\u{1F4E5} Found tokens in URL, setting them...");
@@ -917,6 +1003,14 @@ var BlinkAuth = class {
917
1003
  }
918
1004
  console.log("\u274C No tokens found");
919
1005
  if (this.config.authRequired) {
1006
+ if (this.isIframe && !this.tokenRequestSent) {
1007
+ this.requestTokensFromParent();
1008
+ await new Promise((resolve) => setTimeout(resolve, 500));
1009
+ if (this.parentWindowTokens) {
1010
+ await this.setTokens(this.parentWindowTokens, false);
1011
+ return;
1012
+ }
1013
+ }
920
1014
  console.log("\u{1F504} Auth required, redirecting to auth page...");
921
1015
  this.redirectToAuth();
922
1016
  } else {
@@ -955,6 +1049,12 @@ var BlinkAuth = class {
955
1049
  */
956
1050
  logout(redirectUrl) {
957
1051
  this.clearTokens();
1052
+ if (this.isIframe && window.parent !== window) {
1053
+ window.parent.postMessage({
1054
+ type: "BLINK_AUTH_LOGOUT_IFRAME",
1055
+ projectId: this.config.projectId
1056
+ }, "*");
1057
+ }
958
1058
  if (redirectUrl && typeof window !== "undefined") {
959
1059
  window.location.href = redirectUrl;
960
1060
  }
@@ -1173,7 +1273,7 @@ var BlinkAuth = class {
1173
1273
  token_type: data.token_type,
1174
1274
  expires_in: data.expires_in,
1175
1275
  refresh_expires_in: data.refresh_expires_in
1176
- }, true);
1276
+ }, !this.isIframe);
1177
1277
  return true;
1178
1278
  } catch (error) {
1179
1279
  console.error("Token refresh failed:", error);
@@ -1250,7 +1350,7 @@ var BlinkAuth = class {
1250
1350
  return false;
1251
1351
  }
1252
1352
  } catch (error) {
1253
- console.log("\u{1F4A5} Error validating tokens:", error);
1353
+ console.log("\uFFFD\uFFFD Error validating tokens:", error);
1254
1354
  return false;
1255
1355
  }
1256
1356
  }
@@ -1264,9 +1364,10 @@ var BlinkAuth = class {
1264
1364
  hasAccessToken: !!tokensWithTimestamp.access_token,
1265
1365
  hasRefreshToken: !!tokensWithTimestamp.refresh_token,
1266
1366
  expiresIn: tokensWithTimestamp.expires_in,
1267
- issuedAt: tokensWithTimestamp.issued_at
1367
+ issuedAt: tokensWithTimestamp.issued_at,
1368
+ isIframe: this.isIframe
1268
1369
  });
1269
- if (persist && typeof window !== "undefined") {
1370
+ if (persist && !this.isIframe && typeof window !== "undefined") {
1270
1371
  try {
1271
1372
  localStorage.setItem("blink_tokens", JSON.stringify(tokensWithTimestamp));
1272
1373
  console.log("\u{1F4BE} Tokens persisted to localStorage");
@@ -1317,6 +1418,7 @@ var BlinkAuth = class {
1317
1418
  });
1318
1419
  }
1319
1420
  clearTokens() {
1421
+ this.parentWindowTokens = null;
1320
1422
  if (typeof window !== "undefined") {
1321
1423
  try {
1322
1424
  localStorage.removeItem("blink_tokens");
@@ -1333,13 +1435,17 @@ var BlinkAuth = class {
1333
1435
  }
1334
1436
  getStoredTokens() {
1335
1437
  if (typeof window === "undefined") return null;
1438
+ if (this.isIframe && this.parentWindowTokens) {
1439
+ console.log("\u{1F4E6} Using parent window tokens");
1440
+ return this.parentWindowTokens;
1441
+ }
1336
1442
  try {
1337
1443
  const stored = localStorage.getItem("blink_tokens");
1338
1444
  console.log("\u{1F50D} Checking localStorage for tokens:", {
1339
1445
  hasStoredData: !!stored,
1340
1446
  storedLength: stored?.length || 0,
1341
1447
  origin: window.location.origin,
1342
- isIframe: window.self !== window.top
1448
+ isIframe: this.isIframe
1343
1449
  });
1344
1450
  if (stored) {
1345
1451
  const tokens = JSON.parse(stored);
package/dist/index.mjs CHANGED
@@ -865,6 +865,9 @@ var BlinkAuth = class {
865
865
  authState;
866
866
  listeners = /* @__PURE__ */ new Set();
867
867
  authUrl = "https://blink.new";
868
+ parentWindowTokens = null;
869
+ isIframe = false;
870
+ tokenRequestSent = false;
868
871
  constructor(config) {
869
872
  this.config = config;
870
873
  this.authState = {
@@ -874,9 +877,80 @@ var BlinkAuth = class {
874
877
  isLoading: false
875
878
  };
876
879
  if (typeof window !== "undefined") {
880
+ this.isIframe = window.self !== window.top;
881
+ if (this.isIframe) {
882
+ console.log("\u{1F5BC}\uFE0F Detected iframe environment, setting up parent window communication");
883
+ this.setupParentWindowListener();
884
+ }
877
885
  this.initialize();
878
886
  }
879
887
  }
888
+ /**
889
+ * Setup listener for tokens from parent window (iframe only)
890
+ */
891
+ setupParentWindowListener() {
892
+ if (!this.isIframe) return;
893
+ window.addEventListener("message", (event) => {
894
+ const trustedOrigins = [
895
+ "https://blink.new",
896
+ "http://localhost:3000",
897
+ "http://localhost:3001",
898
+ "https://localhost:3000",
899
+ "https://localhost:3001"
900
+ ];
901
+ if (!trustedOrigins.includes(event.origin)) {
902
+ return;
903
+ }
904
+ if (event.data?.type === "BLINK_AUTH_TOKENS") {
905
+ console.log("\u{1F4E5} Received auth tokens from parent window", {
906
+ hasTokens: !!event.data.tokens,
907
+ projectId: event.data.projectId
908
+ });
909
+ const { tokens, projectId } = event.data;
910
+ if (projectId && projectId !== this.config.projectId) {
911
+ console.log("\u26A0\uFE0F Ignoring tokens for different project:", projectId);
912
+ return;
913
+ }
914
+ if (tokens) {
915
+ this.parentWindowTokens = tokens;
916
+ this.setTokens(tokens, false).then(() => {
917
+ console.log("\u2705 Tokens from parent window applied successfully");
918
+ }).catch((error) => {
919
+ console.error("\u274C Failed to apply parent window tokens:", error);
920
+ });
921
+ }
922
+ }
923
+ if (event.data?.type === "BLINK_AUTH_LOGOUT") {
924
+ console.log("\u{1F4E4} Received logout command from parent window");
925
+ this.clearTokens();
926
+ }
927
+ if (event.data?.type === "BLINK_AUTH_REFRESH") {
928
+ console.log("\u{1F504} Received token refresh from parent window");
929
+ const { tokens } = event.data;
930
+ if (tokens) {
931
+ this.parentWindowTokens = tokens;
932
+ this.setTokens(tokens, false).catch((error) => {
933
+ console.error("\u274C Failed to apply refreshed tokens:", error);
934
+ });
935
+ }
936
+ }
937
+ });
938
+ this.requestTokensFromParent();
939
+ }
940
+ /**
941
+ * Request tokens from parent window
942
+ */
943
+ requestTokensFromParent() {
944
+ if (!this.isIframe || this.tokenRequestSent) return;
945
+ if (window.parent !== window) {
946
+ console.log("\u{1F504} Requesting auth tokens from parent window");
947
+ window.parent.postMessage({
948
+ type: "BLINK_REQUEST_AUTH_TOKENS",
949
+ projectId: this.config.projectId
950
+ }, "*");
951
+ this.tokenRequestSent = true;
952
+ }
953
+ }
880
954
  /**
881
955
  * Initialize authentication from stored tokens or URL fragments
882
956
  */
@@ -884,6 +958,18 @@ var BlinkAuth = class {
884
958
  console.log("\u{1F680} Initializing Blink Auth...");
885
959
  this.setLoading(true);
886
960
  try {
961
+ if (this.isIframe) {
962
+ console.log("\u{1F50D} Detected iframe environment, waiting for parent tokens...");
963
+ for (let i = 0; i < 10; i++) {
964
+ if (this.parentWindowTokens) {
965
+ console.log("\u2705 Using tokens from parent window");
966
+ await this.setTokens(this.parentWindowTokens, false);
967
+ return;
968
+ }
969
+ await new Promise((resolve) => setTimeout(resolve, 100));
970
+ }
971
+ console.log("\u23F0 Timeout waiting for parent tokens, continuing with normal flow...");
972
+ }
887
973
  const tokensFromUrl = this.extractTokensFromUrl();
888
974
  if (tokensFromUrl) {
889
975
  console.log("\u{1F4E5} Found tokens in URL, setting them...");
@@ -915,6 +1001,14 @@ var BlinkAuth = class {
915
1001
  }
916
1002
  console.log("\u274C No tokens found");
917
1003
  if (this.config.authRequired) {
1004
+ if (this.isIframe && !this.tokenRequestSent) {
1005
+ this.requestTokensFromParent();
1006
+ await new Promise((resolve) => setTimeout(resolve, 500));
1007
+ if (this.parentWindowTokens) {
1008
+ await this.setTokens(this.parentWindowTokens, false);
1009
+ return;
1010
+ }
1011
+ }
918
1012
  console.log("\u{1F504} Auth required, redirecting to auth page...");
919
1013
  this.redirectToAuth();
920
1014
  } else {
@@ -953,6 +1047,12 @@ var BlinkAuth = class {
953
1047
  */
954
1048
  logout(redirectUrl) {
955
1049
  this.clearTokens();
1050
+ if (this.isIframe && window.parent !== window) {
1051
+ window.parent.postMessage({
1052
+ type: "BLINK_AUTH_LOGOUT_IFRAME",
1053
+ projectId: this.config.projectId
1054
+ }, "*");
1055
+ }
956
1056
  if (redirectUrl && typeof window !== "undefined") {
957
1057
  window.location.href = redirectUrl;
958
1058
  }
@@ -1171,7 +1271,7 @@ var BlinkAuth = class {
1171
1271
  token_type: data.token_type,
1172
1272
  expires_in: data.expires_in,
1173
1273
  refresh_expires_in: data.refresh_expires_in
1174
- }, true);
1274
+ }, !this.isIframe);
1175
1275
  return true;
1176
1276
  } catch (error) {
1177
1277
  console.error("Token refresh failed:", error);
@@ -1248,7 +1348,7 @@ var BlinkAuth = class {
1248
1348
  return false;
1249
1349
  }
1250
1350
  } catch (error) {
1251
- console.log("\u{1F4A5} Error validating tokens:", error);
1351
+ console.log("\uFFFD\uFFFD Error validating tokens:", error);
1252
1352
  return false;
1253
1353
  }
1254
1354
  }
@@ -1262,9 +1362,10 @@ var BlinkAuth = class {
1262
1362
  hasAccessToken: !!tokensWithTimestamp.access_token,
1263
1363
  hasRefreshToken: !!tokensWithTimestamp.refresh_token,
1264
1364
  expiresIn: tokensWithTimestamp.expires_in,
1265
- issuedAt: tokensWithTimestamp.issued_at
1365
+ issuedAt: tokensWithTimestamp.issued_at,
1366
+ isIframe: this.isIframe
1266
1367
  });
1267
- if (persist && typeof window !== "undefined") {
1368
+ if (persist && !this.isIframe && typeof window !== "undefined") {
1268
1369
  try {
1269
1370
  localStorage.setItem("blink_tokens", JSON.stringify(tokensWithTimestamp));
1270
1371
  console.log("\u{1F4BE} Tokens persisted to localStorage");
@@ -1315,6 +1416,7 @@ var BlinkAuth = class {
1315
1416
  });
1316
1417
  }
1317
1418
  clearTokens() {
1419
+ this.parentWindowTokens = null;
1318
1420
  if (typeof window !== "undefined") {
1319
1421
  try {
1320
1422
  localStorage.removeItem("blink_tokens");
@@ -1331,13 +1433,17 @@ var BlinkAuth = class {
1331
1433
  }
1332
1434
  getStoredTokens() {
1333
1435
  if (typeof window === "undefined") return null;
1436
+ if (this.isIframe && this.parentWindowTokens) {
1437
+ console.log("\u{1F4E6} Using parent window tokens");
1438
+ return this.parentWindowTokens;
1439
+ }
1334
1440
  try {
1335
1441
  const stored = localStorage.getItem("blink_tokens");
1336
1442
  console.log("\u{1F50D} Checking localStorage for tokens:", {
1337
1443
  hasStoredData: !!stored,
1338
1444
  storedLength: stored?.length || 0,
1339
1445
  origin: window.location.origin,
1340
- isIframe: window.self !== window.top
1446
+ isIframe: this.isIframe
1341
1447
  });
1342
1448
  if (stored) {
1343
1449
  const tokens = JSON.parse(stored);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blinkdotnew/sdk",
3
- "version": "0.14.1",
3
+ "version": "0.14.2",
4
4
  "description": "Blink TypeScript SDK for client-side applications - Zero-boilerplate CRUD + auth + AI + analytics + notifications for modern SaaS/AI apps",
5
5
  "keywords": [
6
6
  "blink",