naystack 1.8.18 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +94 -34
  2. package/dist/auth/client.cjs.js +2 -2
  3. package/dist/auth/client.esm.js +2 -2
  4. package/dist/auth/email/client.cjs.js +2 -2
  5. package/dist/auth/email/client.esm.js +2 -2
  6. package/dist/auth/email/index.cjs.js +2 -2
  7. package/dist/auth/email/index.esm.js +2 -2
  8. package/dist/auth/email/next.cjs.js +2 -2
  9. package/dist/auth/email/next.esm.js +2 -2
  10. package/dist/auth/email/routes/delete.cjs.js +2 -2
  11. package/dist/auth/email/routes/delete.esm.js +2 -2
  12. package/dist/auth/email/routes/get.cjs.js +2 -2
  13. package/dist/auth/email/routes/get.esm.js +2 -2
  14. package/dist/auth/email/routes/post.cjs.js +2 -2
  15. package/dist/auth/email/routes/post.esm.js +2 -2
  16. package/dist/auth/email/routes/put.cjs.js +2 -2
  17. package/dist/auth/email/routes/put.esm.js +2 -2
  18. package/dist/auth/email/token.cjs.js +2 -2
  19. package/dist/auth/email/token.esm.js +2 -2
  20. package/dist/auth/email/utils.cjs.js +2 -2
  21. package/dist/auth/email/utils.esm.js +2 -2
  22. package/dist/auth/google/get.cjs.js +2 -2
  23. package/dist/auth/google/get.esm.js +2 -2
  24. package/dist/auth/google/index.cjs.js +2 -2
  25. package/dist/auth/google/index.esm.js +2 -2
  26. package/dist/auth/index.cjs.js +4 -297
  27. package/dist/auth/index.d.mts +0 -5
  28. package/dist/auth/index.d.ts +0 -5
  29. package/dist/auth/index.esm.js +3 -293
  30. package/dist/auth/token-store.cjs.js +2 -2
  31. package/dist/auth/token-store.esm.js +2 -2
  32. package/dist/env.cjs.js +3 -3
  33. package/dist/env.d.mts +3 -2
  34. package/dist/env.d.ts +3 -2
  35. package/dist/env.esm.js +3 -3
  36. package/dist/file/client.cjs.js +2 -2
  37. package/dist/file/client.esm.js +2 -2
  38. package/dist/file/index.cjs.js +2 -2
  39. package/dist/file/index.esm.js +2 -2
  40. package/dist/file/put.cjs.js +2 -2
  41. package/dist/file/put.esm.js +2 -2
  42. package/dist/file/setup.cjs.js +2 -2
  43. package/dist/file/setup.esm.js +2 -2
  44. package/dist/file/utils.cjs.js +2 -2
  45. package/dist/file/utils.esm.js +2 -2
  46. package/dist/graphql/client.cjs.js +2 -2
  47. package/dist/graphql/client.esm.js +2 -2
  48. package/dist/graphql/index.cjs.js +2 -2
  49. package/dist/graphql/index.esm.js +2 -2
  50. package/dist/graphql/init.cjs.js +2 -2
  51. package/dist/graphql/init.esm.js +2 -2
  52. package/dist/graphql/links.cjs.js +2 -2
  53. package/dist/graphql/links.esm.js +2 -2
  54. package/dist/graphql/next.cjs.js +2 -2
  55. package/dist/graphql/next.esm.js +2 -2
  56. package/dist/graphql/server.cjs.js +2 -2
  57. package/dist/graphql/server.esm.js +2 -2
  58. package/dist/graphql/utils.cjs.js +2 -2
  59. package/dist/graphql/utils.esm.js +2 -2
  60. package/dist/socials/connect.cjs.js +151 -0
  61. package/dist/socials/connect.d.mts +70 -0
  62. package/dist/socials/connect.d.ts +70 -0
  63. package/dist/socials/connect.esm.js +126 -0
  64. package/dist/socials/index.cjs.js +367 -35
  65. package/dist/socials/index.d.mts +8 -2
  66. package/dist/socials/index.d.ts +8 -2
  67. package/dist/socials/index.esm.js +357 -34
  68. package/dist/socials/instagram/adapter.cjs.js +275 -0
  69. package/dist/socials/instagram/adapter.d.mts +30 -0
  70. package/dist/socials/instagram/adapter.d.ts +30 -0
  71. package/dist/socials/instagram/adapter.esm.js +248 -0
  72. package/dist/{auth/instagram/utils.cjs.js → socials/instagram/auth.cjs.js} +43 -41
  73. package/dist/socials/instagram/auth.d.mts +52 -0
  74. package/dist/socials/instagram/auth.d.ts +52 -0
  75. package/dist/{auth/instagram/utils.esm.js → socials/instagram/auth.esm.js} +37 -35
  76. package/dist/socials/instagram/getters.d.mts +1 -0
  77. package/dist/socials/instagram/getters.d.ts +1 -0
  78. package/dist/socials/instagram/setters.cjs.js +56 -31
  79. package/dist/socials/instagram/setters.d.mts +1 -0
  80. package/dist/socials/instagram/setters.d.ts +1 -0
  81. package/dist/socials/instagram/setters.esm.js +56 -31
  82. package/dist/socials/instagram/types.d.mts +2 -1
  83. package/dist/socials/instagram/types.d.ts +2 -1
  84. package/dist/socials/meta/container.cjs.js +57 -30
  85. package/dist/socials/meta/container.d.mts +15 -28
  86. package/dist/socials/meta/container.d.ts +15 -28
  87. package/dist/socials/meta/container.esm.js +56 -31
  88. package/dist/socials/provider.cjs.js +18 -0
  89. package/dist/socials/provider.d.mts +52 -0
  90. package/dist/socials/provider.d.ts +52 -0
  91. package/dist/socials/provider.esm.js +0 -0
  92. package/dist/socials/threads/getters.d.mts +1 -0
  93. package/dist/socials/threads/getters.d.ts +1 -0
  94. package/dist/socials/threads/setters.cjs.js +56 -31
  95. package/dist/socials/threads/setters.d.mts +1 -0
  96. package/dist/socials/threads/setters.d.ts +1 -0
  97. package/dist/socials/threads/setters.esm.js +56 -31
  98. package/dist/socials/threads/types.d.mts +2 -1
  99. package/dist/socials/threads/types.d.ts +2 -1
  100. package/dist/socials/types.cjs.js +43 -0
  101. package/dist/socials/types.d.mts +99 -0
  102. package/dist/socials/types.d.ts +99 -0
  103. package/dist/socials/types.esm.js +17 -0
  104. package/dist/socials/utils/poll.cjs.js +59 -0
  105. package/dist/socials/utils/poll.d.mts +85 -0
  106. package/dist/socials/utils/poll.d.ts +85 -0
  107. package/dist/socials/utils/poll.esm.js +33 -0
  108. package/dist/utils/client/index.cjs.js +2 -2
  109. package/dist/utils/client/index.esm.js +2 -2
  110. package/dist/utils/client/seo.cjs.js +2 -2
  111. package/dist/utils/client/seo.esm.js +2 -2
  112. package/package.json +1 -1
  113. package/dist/auth/instagram/index.cjs.js +0 -379
  114. package/dist/auth/instagram/index.d.mts +0 -58
  115. package/dist/auth/instagram/index.d.ts +0 -58
  116. package/dist/auth/instagram/index.esm.js +0 -350
  117. package/dist/auth/instagram/route.cjs.js +0 -359
  118. package/dist/auth/instagram/route.d.mts +0 -15
  119. package/dist/auth/instagram/route.d.ts +0 -15
  120. package/dist/auth/instagram/route.esm.js +0 -334
  121. package/dist/auth/instagram/utils.d.mts +0 -57
  122. package/dist/auth/instagram/utils.d.ts +0 -57
@@ -34,13 +34,10 @@ __export(auth_exports, {
34
34
  AuthWrapper: () => AuthWrapper,
35
35
  checkAuthStatus: () => checkAuthStatus,
36
36
  getContext: () => getContext,
37
- getInstagramAuthorizationURL: () => getInstagramAuthorizationURL,
38
37
  getRefreshToken: () => getRefreshToken,
39
- getRefreshedInstagramAccessToken: () => getRefreshedInstagramAccessToken,
40
38
  getTokenizedResponse: () => getTokenizedResponse,
41
39
  setupEmailAuth: () => setupEmailAuth,
42
- setupGoogleAuth: () => setupGoogleAuth,
43
- setupInstagramAuth: () => setupInstagramAuth
40
+ setupGoogleAuth: () => setupGoogleAuth
44
41
  });
45
42
  module.exports = __toCommonJS(auth_exports);
46
43
 
@@ -107,8 +104,8 @@ var getEnvValue = (key) => {
107
104
  return process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT || EXTRA_ENV.EMAIL_AUTH_ENDPOINT;
108
105
  case "NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT" /* NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT */:
109
106
  return process.env.NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT || EXTRA_ENV.GOOGLE_AUTH_ENDPOINT;
110
- case "NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */:
111
- return process.env.NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT || EXTRA_ENV.INSTAGRAM_AUTH_ENDPOINT;
107
+ case "NEXT_PUBLIC_SOCIAL_AUTH_ENDPOINT" /* NEXT_PUBLIC_SOCIAL_AUTH_ENDPOINT */:
108
+ return process.env.NEXT_PUBLIC_SOCIAL_AUTH_ENDPOINT || EXTRA_ENV.SOCIAL_AUTH_ENDPOINT;
112
109
  case "NEXT_PUBLIC_FILE_ENDPOINT" /* NEXT_PUBLIC_FILE_ENDPOINT */:
113
110
  return process.env.NEXT_PUBLIC_FILE_ENDPOINT || EXTRA_ENV.FILE_ENDPOINT;
114
111
  case "NEXT_PUBLIC_BASE_URL" /* NEXT_PUBLIC_BASE_URL */:
@@ -192,18 +189,6 @@ function getUserIdFromRefreshToken(refreshToken) {
192
189
  }
193
190
  return null;
194
191
  }
195
- function getUserIdFromAccessToken(accessToken) {
196
- if (accessToken)
197
- try {
198
- const decoded = (0, import_jsonwebtoken.verify)(accessToken, getEnv("SIGNING_KEY" /* SIGNING_KEY */));
199
- if (typeof decoded !== "string" && typeof decoded.id === "number")
200
- return decoded.id;
201
- } catch (e) {
202
- if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
203
- return null;
204
- }
205
- return null;
206
- }
207
192
  function verifyUser(user, password) {
208
193
  if (!user.password) return false;
209
194
  return (0, import_bcryptjs.compare)(password, user.password);
@@ -574,281 +559,6 @@ function setupGoogleAuth(props) {
574
559
  };
575
560
  }
576
561
 
577
- // src/auth/instagram/route.ts
578
- var import_server8 = require("next/server");
579
-
580
- // src/auth/instagram/utils.ts
581
- async function getRefreshedInstagramAccessToken(token) {
582
- const request = await fetch(
583
- `https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=${token}`
584
- );
585
- const response = await request.json();
586
- return response.access_token;
587
- }
588
- async function getLongLivedToken(code, redirectURL, clientId, clientSecret) {
589
- const formData = new FormData();
590
- formData.set("client_id", clientId);
591
- formData.set("client_secret", clientSecret);
592
- formData.set("grant_type", "authorization_code");
593
- formData.set("redirect_uri", redirectURL);
594
- formData.set("code", code);
595
- const shortRes = await fetch("https://api.instagram.com/oauth/access_token", {
596
- method: "POST",
597
- body: formData
598
- });
599
- if (shortRes.ok) {
600
- const shortResData = await shortRes.json();
601
- if (shortResData.access_token) {
602
- const shortLivedToken = shortResData.access_token;
603
- const longRes = await fetch(
604
- `https://graph.instagram.com/access_token?grant_type=ig_exchange_token&access_token=${shortLivedToken}&client_secret=${getEnv("INSTAGRAM_CLIENT_SECRET" /* INSTAGRAM_CLIENT_SECRET */)}`
605
- );
606
- if (longRes.ok) {
607
- const longResData = await longRes.json();
608
- return {
609
- accessToken: longResData.access_token,
610
- userId: shortResData.user_id
611
- };
612
- }
613
- }
614
- }
615
- }
616
- var getInstagramAuthorizationURL = (token, scopes = ["instagram_business_basic"]) => `https://www.instagram.com/oauth/authorize/?client_id=${getEnv(
617
- "INSTAGRAM_CLIENT_ID" /* INSTAGRAM_CLIENT_ID */
618
- )}&response_type=code&enable_fb_login=0&force_authentication=1&scope=${scopes.join(",")}&state=${token}&redirect_uri=${getEnv("NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */)}#weblink`;
619
-
620
- // src/socials/meta/request.ts
621
- function createGraphClient(baseURL) {
622
- const buildURL = (token, path, params = {}) => {
623
- const search = new URLSearchParams();
624
- for (const [key, value] of Object.entries(params)) {
625
- if (value !== void 0) search.set(key, String(value));
626
- }
627
- search.set("access_token", token);
628
- return `${baseURL}/${path}?${search.toString()}`;
629
- };
630
- const request = async (token, path, options = {}) => {
631
- const response = await fetch(buildURL(token, path, options.params), {
632
- method: options.method || (options.body ? "POST" : "GET"),
633
- headers: {
634
- Authorization: `Bearer ${token}`,
635
- ...options.body ? { "Content-Type": "application/json" } : {}
636
- },
637
- ...options.body ? { body: JSON.stringify(options.body) } : {}
638
- });
639
- return response.json().catch(() => null);
640
- };
641
- return { buildURL, request };
642
- }
643
- function readGraphID(label, result) {
644
- if (result?.error) {
645
- console.error(`[naystack] ${label} failed: ${result.error.message}`);
646
- return null;
647
- }
648
- return result?.id || null;
649
- }
650
-
651
- // src/socials/instagram/utils.ts
652
- var client = createGraphClient("https://graph.instagram.com/v23.0");
653
- var getInstagramData = client.request;
654
-
655
- // src/socials/instagram/getters.ts
656
- var getInstagramUser = (token, id, fields) => {
657
- return getInstagramData(token, id || "me", {
658
- params: {
659
- fields: fields ? fields.join(",") : "username,followers_count,media_count"
660
- }
661
- });
662
- };
663
- var getInstagramContainerStatus = async (token, id) => {
664
- const result = await getInstagramData(token, id, { params: { fields: "status_code,status" } });
665
- if (!result?.status_code) return null;
666
- return {
667
- status: result.status_code,
668
- error: result.status_code === "ERROR" ? result.status : void 0
669
- };
670
- };
671
-
672
- // src/socials/meta/container.ts
673
- var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
674
- async function waitForContainer(getState, { intervalMS = 5e3, timeoutMS = 3e5 } = {}) {
675
- const deadline = Date.now() + timeoutMS;
676
- let state = await getState();
677
- if (!state) {
678
- await sleep(2e3);
679
- return null;
680
- }
681
- while (state?.status === "IN_PROGRESS" && Date.now() < deadline) {
682
- await sleep(intervalMS);
683
- state = await getState();
684
- }
685
- return state;
686
- }
687
- function createPublisher(platform) {
688
- const isReady = async (token, id, label, wait) => {
689
- const state = await waitForContainer(
690
- () => platform.getStatus(token, id),
691
- wait
692
- );
693
- if (state && state.status !== "FINISHED" && state.status !== "PUBLISHED") {
694
- console.error(
695
- `[naystack] ${platform.name} ${label} ${id} is ${state.status}${state.error ? `: ${state.error}` : ""}`
696
- );
697
- return false;
698
- }
699
- return true;
700
- };
701
- const createReady = async (token, params, label, wait, retry) => {
702
- const attempts = Math.max(1, retry?.attempts ?? 3);
703
- const backoffMS = retry?.backoffMS ?? 2e3;
704
- for (let attempt = 1; attempt <= attempts; attempt++) {
705
- const id = await platform.createContainer(token, params);
706
- if (id && await isReady(token, id, label, wait)) return id;
707
- if (attempt < attempts) {
708
- const delay = backoffMS * 2 ** (attempt - 1);
709
- console.warn(
710
- `[naystack] ${platform.name} ${label} attempt ${attempt}/${attempts} failed \u2014 retrying in ${delay}ms`
711
- );
712
- await sleep(delay);
713
- }
714
- }
715
- return null;
716
- };
717
- return {
718
- /** Creates a container, waits for it to finish processing, then publishes it. */
719
- publish: async (token, params, wait, retry) => {
720
- const containerID = await createReady(
721
- token,
722
- params,
723
- "container",
724
- wait,
725
- retry
726
- );
727
- if (!containerID) return null;
728
- return platform.publish(token, containerID);
729
- },
730
- /** Creates and awaits every carousel child, resolving to their ids — or `null` if any failed. */
731
- createChildren: async (token, items, wait, retry) => {
732
- const children = [];
733
- for (const item of items) {
734
- const childID = await createReady(
735
- token,
736
- { ...item, is_carousel_item: true },
737
- "carousel item",
738
- wait,
739
- retry
740
- );
741
- if (!childID) return null;
742
- children.push(childID);
743
- }
744
- return children;
745
- }
746
- };
747
- }
748
-
749
- // src/socials/instagram/setters.ts
750
- var publisher = createPublisher({
751
- name: "Instagram",
752
- getStatus: getInstagramContainerStatus,
753
- createContainer: async (token, params) => readGraphID(
754
- "Instagram media container",
755
- await getInstagramData(token, "me/media", {
756
- params,
757
- method: "POST"
758
- })
759
- ),
760
- publish: async (token, creationID) => readGraphID(
761
- "Instagram publish",
762
- await getInstagramData(token, "me/media_publish", {
763
- params: { creation_id: creationID },
764
- method: "POST"
765
- })
766
- )
767
- });
768
-
769
- // src/socials/meta/webhook.ts
770
- var import_server7 = require("next/server");
771
-
772
- // src/socials/threads/utils.ts
773
- var client2 = createGraphClient("https://graph.threads.net/v1.0");
774
- var getThreadsData = client2.request;
775
-
776
- // src/socials/threads/getters.ts
777
- var getThreadsContainerStatus = async (token, id) => {
778
- const result = await getThreadsData(token, id, { params: { fields: "status,error_message" } });
779
- if (!result?.status) return null;
780
- return { status: result.status, error: result.error_message };
781
- };
782
-
783
- // src/socials/threads/setters.ts
784
- var publisher2 = createPublisher({
785
- name: "Threads",
786
- getStatus: getThreadsContainerStatus,
787
- createContainer: async (token, params) => readGraphID(
788
- "Threads container",
789
- await getThreadsData(token, "me/threads", {
790
- params,
791
- method: "POST"
792
- })
793
- ),
794
- publish: async (token, creationID) => readGraphID(
795
- "Threads publish",
796
- await getThreadsData(token, "me/threads_publish", {
797
- params: { creation_id: creationID },
798
- method: "POST"
799
- })
800
- )
801
- });
802
-
803
- // src/auth/instagram/route.ts
804
- var getInstagramRoute = ({
805
- redirectURL,
806
- errorRedirectURL,
807
- onUser,
808
- scopes
809
- }) => {
810
- const handleError2 = (message) => import_server8.NextResponse.redirect(`${errorRedirectURL}?error=${message}`);
811
- return async (req) => {
812
- const accessCode = req.nextUrl.searchParams.get("code");
813
- const error = req.nextUrl.searchParams.get("error");
814
- const stateToken = req.nextUrl.searchParams.get("state");
815
- if (error) return handleError2(error);
816
- if (!accessCode) {
817
- if (!stateToken) return handleError2("Invalid request");
818
- return import_server8.NextResponse.redirect(
819
- getInstagramAuthorizationURL(stateToken, scopes),
820
- 302
821
- );
822
- }
823
- if (!stateToken) return handleError2("Invalid request");
824
- const instagramData = await getLongLivedToken(
825
- accessCode,
826
- getEnv("NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */),
827
- getEnv("INSTAGRAM_CLIENT_ID" /* INSTAGRAM_CLIENT_ID */),
828
- getEnv("INSTAGRAM_CLIENT_SECRET" /* INSTAGRAM_CLIENT_SECRET */)
829
- );
830
- if (!instagramData?.accessToken)
831
- return handleError2("Unable to reach Instagram");
832
- const personalInfo = await getInstagramUser(instagramData.accessToken);
833
- if (!personalInfo?.username) return handleError2("You are not logged in");
834
- const loggedInUserID = getUserIdFromAccessToken(stateToken);
835
- const errorMessage = await onUser(
836
- personalInfo,
837
- loggedInUserID,
838
- instagramData.accessToken
839
- );
840
- if (errorMessage) return handleError2(errorMessage);
841
- return import_server8.NextResponse.redirect(redirectURL);
842
- };
843
- };
844
-
845
- // src/auth/instagram/index.ts
846
- function setupInstagramAuth(props) {
847
- return {
848
- GET: getInstagramRoute(props)
849
- };
850
- }
851
-
852
562
  // src/auth/utils/token.ts
853
563
  var import_headers4 = require("next/headers");
854
564
  async function getRefreshToken() {
@@ -861,11 +571,8 @@ async function getRefreshToken() {
861
571
  AuthWrapper,
862
572
  checkAuthStatus,
863
573
  getContext,
864
- getInstagramAuthorizationURL,
865
574
  getRefreshToken,
866
- getRefreshedInstagramAccessToken,
867
575
  getTokenizedResponse,
868
576
  setupEmailAuth,
869
- setupGoogleAuth,
870
- setupInstagramAuth
577
+ setupGoogleAuth
871
578
  });
@@ -1,18 +1,13 @@
1
1
  export { setupEmailAuth } from './email/index.mjs';
2
2
  export { SetupGoogleAuthOptions, setupGoogleAuth } from './google/index.mjs';
3
- export { SetupInstagramAuthOptions, setupInstagramAuth } from './instagram/index.mjs';
4
3
  export { getRefreshToken } from './utils/token.mjs';
5
4
  export { AuthFetch, AuthWrapper } from './email/next.mjs';
6
5
  export { checkAuthStatus, getTokenizedResponse } from './email/token.mjs';
7
6
  export { getContext } from './email/utils.mjs';
8
- export { InstagramScope, getInstagramAuthorizationURL, getRefreshedInstagramAccessToken } from './instagram/utils.mjs';
9
7
  import 'next/server';
10
8
  import './email/types.mjs';
11
9
  import './types.mjs';
12
10
  import 'googleapis';
13
- import '../socials/instagram/types.mjs';
14
- import '../socials/meta/container.mjs';
15
- import '../socials/meta/types.mjs';
16
11
  import 'react';
17
12
  import './email/client.mjs';
18
13
  import 'naystack/auth/token-store';
@@ -1,18 +1,13 @@
1
1
  export { setupEmailAuth } from './email/index.js';
2
2
  export { SetupGoogleAuthOptions, setupGoogleAuth } from './google/index.js';
3
- export { SetupInstagramAuthOptions, setupInstagramAuth } from './instagram/index.js';
4
3
  export { getRefreshToken } from './utils/token.js';
5
4
  export { AuthFetch, AuthWrapper } from './email/next.js';
6
5
  export { checkAuthStatus, getTokenizedResponse } from './email/token.js';
7
6
  export { getContext } from './email/utils.js';
8
- export { InstagramScope, getInstagramAuthorizationURL, getRefreshedInstagramAccessToken } from './instagram/utils.js';
9
7
  import 'next/server';
10
8
  import './email/types.js';
11
9
  import './types.js';
12
10
  import 'googleapis';
13
- import '../socials/instagram/types.js';
14
- import '../socials/meta/container.js';
15
- import '../socials/meta/types.js';
16
11
  import 'react';
17
12
  import './email/client.js';
18
13
  import 'naystack/auth/token-store';
@@ -61,8 +61,8 @@ var getEnvValue = (key) => {
61
61
  return process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT || EXTRA_ENV.EMAIL_AUTH_ENDPOINT;
62
62
  case "NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT" /* NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT */:
63
63
  return process.env.NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT || EXTRA_ENV.GOOGLE_AUTH_ENDPOINT;
64
- case "NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */:
65
- return process.env.NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT || EXTRA_ENV.INSTAGRAM_AUTH_ENDPOINT;
64
+ case "NEXT_PUBLIC_SOCIAL_AUTH_ENDPOINT" /* NEXT_PUBLIC_SOCIAL_AUTH_ENDPOINT */:
65
+ return process.env.NEXT_PUBLIC_SOCIAL_AUTH_ENDPOINT || EXTRA_ENV.SOCIAL_AUTH_ENDPOINT;
66
66
  case "NEXT_PUBLIC_FILE_ENDPOINT" /* NEXT_PUBLIC_FILE_ENDPOINT */:
67
67
  return process.env.NEXT_PUBLIC_FILE_ENDPOINT || EXTRA_ENV.FILE_ENDPOINT;
68
68
  case "NEXT_PUBLIC_BASE_URL" /* NEXT_PUBLIC_BASE_URL */:
@@ -146,18 +146,6 @@ function getUserIdFromRefreshToken(refreshToken) {
146
146
  }
147
147
  return null;
148
148
  }
149
- function getUserIdFromAccessToken(accessToken) {
150
- if (accessToken)
151
- try {
152
- const decoded = verify(accessToken, getEnv("SIGNING_KEY" /* SIGNING_KEY */));
153
- if (typeof decoded !== "string" && typeof decoded.id === "number")
154
- return decoded.id;
155
- } catch (e) {
156
- if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
157
- return null;
158
- }
159
- return null;
160
- }
161
149
  function verifyUser(user, password) {
162
150
  if (!user.password) return false;
163
151
  return compare(password, user.password);
@@ -532,281 +520,6 @@ function setupGoogleAuth(props) {
532
520
  };
533
521
  }
534
522
 
535
- // src/auth/instagram/route.ts
536
- import { NextResponse as NextResponse7 } from "next/server";
537
-
538
- // src/auth/instagram/utils.ts
539
- async function getRefreshedInstagramAccessToken(token) {
540
- const request = await fetch(
541
- `https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=${token}`
542
- );
543
- const response = await request.json();
544
- return response.access_token;
545
- }
546
- async function getLongLivedToken(code, redirectURL, clientId, clientSecret) {
547
- const formData = new FormData();
548
- formData.set("client_id", clientId);
549
- formData.set("client_secret", clientSecret);
550
- formData.set("grant_type", "authorization_code");
551
- formData.set("redirect_uri", redirectURL);
552
- formData.set("code", code);
553
- const shortRes = await fetch("https://api.instagram.com/oauth/access_token", {
554
- method: "POST",
555
- body: formData
556
- });
557
- if (shortRes.ok) {
558
- const shortResData = await shortRes.json();
559
- if (shortResData.access_token) {
560
- const shortLivedToken = shortResData.access_token;
561
- const longRes = await fetch(
562
- `https://graph.instagram.com/access_token?grant_type=ig_exchange_token&access_token=${shortLivedToken}&client_secret=${getEnv("INSTAGRAM_CLIENT_SECRET" /* INSTAGRAM_CLIENT_SECRET */)}`
563
- );
564
- if (longRes.ok) {
565
- const longResData = await longRes.json();
566
- return {
567
- accessToken: longResData.access_token,
568
- userId: shortResData.user_id
569
- };
570
- }
571
- }
572
- }
573
- }
574
- var getInstagramAuthorizationURL = (token, scopes = ["instagram_business_basic"]) => `https://www.instagram.com/oauth/authorize/?client_id=${getEnv(
575
- "INSTAGRAM_CLIENT_ID" /* INSTAGRAM_CLIENT_ID */
576
- )}&response_type=code&enable_fb_login=0&force_authentication=1&scope=${scopes.join(",")}&state=${token}&redirect_uri=${getEnv("NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */)}#weblink`;
577
-
578
- // src/socials/meta/request.ts
579
- function createGraphClient(baseURL) {
580
- const buildURL = (token, path, params = {}) => {
581
- const search = new URLSearchParams();
582
- for (const [key, value] of Object.entries(params)) {
583
- if (value !== void 0) search.set(key, String(value));
584
- }
585
- search.set("access_token", token);
586
- return `${baseURL}/${path}?${search.toString()}`;
587
- };
588
- const request = async (token, path, options = {}) => {
589
- const response = await fetch(buildURL(token, path, options.params), {
590
- method: options.method || (options.body ? "POST" : "GET"),
591
- headers: {
592
- Authorization: `Bearer ${token}`,
593
- ...options.body ? { "Content-Type": "application/json" } : {}
594
- },
595
- ...options.body ? { body: JSON.stringify(options.body) } : {}
596
- });
597
- return response.json().catch(() => null);
598
- };
599
- return { buildURL, request };
600
- }
601
- function readGraphID(label, result) {
602
- if (result?.error) {
603
- console.error(`[naystack] ${label} failed: ${result.error.message}`);
604
- return null;
605
- }
606
- return result?.id || null;
607
- }
608
-
609
- // src/socials/instagram/utils.ts
610
- var client = createGraphClient("https://graph.instagram.com/v23.0");
611
- var getInstagramData = client.request;
612
-
613
- // src/socials/instagram/getters.ts
614
- var getInstagramUser = (token, id, fields) => {
615
- return getInstagramData(token, id || "me", {
616
- params: {
617
- fields: fields ? fields.join(",") : "username,followers_count,media_count"
618
- }
619
- });
620
- };
621
- var getInstagramContainerStatus = async (token, id) => {
622
- const result = await getInstagramData(token, id, { params: { fields: "status_code,status" } });
623
- if (!result?.status_code) return null;
624
- return {
625
- status: result.status_code,
626
- error: result.status_code === "ERROR" ? result.status : void 0
627
- };
628
- };
629
-
630
- // src/socials/meta/container.ts
631
- var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
632
- async function waitForContainer(getState, { intervalMS = 5e3, timeoutMS = 3e5 } = {}) {
633
- const deadline = Date.now() + timeoutMS;
634
- let state = await getState();
635
- if (!state) {
636
- await sleep(2e3);
637
- return null;
638
- }
639
- while (state?.status === "IN_PROGRESS" && Date.now() < deadline) {
640
- await sleep(intervalMS);
641
- state = await getState();
642
- }
643
- return state;
644
- }
645
- function createPublisher(platform) {
646
- const isReady = async (token, id, label, wait) => {
647
- const state = await waitForContainer(
648
- () => platform.getStatus(token, id),
649
- wait
650
- );
651
- if (state && state.status !== "FINISHED" && state.status !== "PUBLISHED") {
652
- console.error(
653
- `[naystack] ${platform.name} ${label} ${id} is ${state.status}${state.error ? `: ${state.error}` : ""}`
654
- );
655
- return false;
656
- }
657
- return true;
658
- };
659
- const createReady = async (token, params, label, wait, retry) => {
660
- const attempts = Math.max(1, retry?.attempts ?? 3);
661
- const backoffMS = retry?.backoffMS ?? 2e3;
662
- for (let attempt = 1; attempt <= attempts; attempt++) {
663
- const id = await platform.createContainer(token, params);
664
- if (id && await isReady(token, id, label, wait)) return id;
665
- if (attempt < attempts) {
666
- const delay = backoffMS * 2 ** (attempt - 1);
667
- console.warn(
668
- `[naystack] ${platform.name} ${label} attempt ${attempt}/${attempts} failed \u2014 retrying in ${delay}ms`
669
- );
670
- await sleep(delay);
671
- }
672
- }
673
- return null;
674
- };
675
- return {
676
- /** Creates a container, waits for it to finish processing, then publishes it. */
677
- publish: async (token, params, wait, retry) => {
678
- const containerID = await createReady(
679
- token,
680
- params,
681
- "container",
682
- wait,
683
- retry
684
- );
685
- if (!containerID) return null;
686
- return platform.publish(token, containerID);
687
- },
688
- /** Creates and awaits every carousel child, resolving to their ids — or `null` if any failed. */
689
- createChildren: async (token, items, wait, retry) => {
690
- const children = [];
691
- for (const item of items) {
692
- const childID = await createReady(
693
- token,
694
- { ...item, is_carousel_item: true },
695
- "carousel item",
696
- wait,
697
- retry
698
- );
699
- if (!childID) return null;
700
- children.push(childID);
701
- }
702
- return children;
703
- }
704
- };
705
- }
706
-
707
- // src/socials/instagram/setters.ts
708
- var publisher = createPublisher({
709
- name: "Instagram",
710
- getStatus: getInstagramContainerStatus,
711
- createContainer: async (token, params) => readGraphID(
712
- "Instagram media container",
713
- await getInstagramData(token, "me/media", {
714
- params,
715
- method: "POST"
716
- })
717
- ),
718
- publish: async (token, creationID) => readGraphID(
719
- "Instagram publish",
720
- await getInstagramData(token, "me/media_publish", {
721
- params: { creation_id: creationID },
722
- method: "POST"
723
- })
724
- )
725
- });
726
-
727
- // src/socials/meta/webhook.ts
728
- import { NextResponse as NextResponse6 } from "next/server";
729
-
730
- // src/socials/threads/utils.ts
731
- var client2 = createGraphClient("https://graph.threads.net/v1.0");
732
- var getThreadsData = client2.request;
733
-
734
- // src/socials/threads/getters.ts
735
- var getThreadsContainerStatus = async (token, id) => {
736
- const result = await getThreadsData(token, id, { params: { fields: "status,error_message" } });
737
- if (!result?.status) return null;
738
- return { status: result.status, error: result.error_message };
739
- };
740
-
741
- // src/socials/threads/setters.ts
742
- var publisher2 = createPublisher({
743
- name: "Threads",
744
- getStatus: getThreadsContainerStatus,
745
- createContainer: async (token, params) => readGraphID(
746
- "Threads container",
747
- await getThreadsData(token, "me/threads", {
748
- params,
749
- method: "POST"
750
- })
751
- ),
752
- publish: async (token, creationID) => readGraphID(
753
- "Threads publish",
754
- await getThreadsData(token, "me/threads_publish", {
755
- params: { creation_id: creationID },
756
- method: "POST"
757
- })
758
- )
759
- });
760
-
761
- // src/auth/instagram/route.ts
762
- var getInstagramRoute = ({
763
- redirectURL,
764
- errorRedirectURL,
765
- onUser,
766
- scopes
767
- }) => {
768
- const handleError2 = (message) => NextResponse7.redirect(`${errorRedirectURL}?error=${message}`);
769
- return async (req) => {
770
- const accessCode = req.nextUrl.searchParams.get("code");
771
- const error = req.nextUrl.searchParams.get("error");
772
- const stateToken = req.nextUrl.searchParams.get("state");
773
- if (error) return handleError2(error);
774
- if (!accessCode) {
775
- if (!stateToken) return handleError2("Invalid request");
776
- return NextResponse7.redirect(
777
- getInstagramAuthorizationURL(stateToken, scopes),
778
- 302
779
- );
780
- }
781
- if (!stateToken) return handleError2("Invalid request");
782
- const instagramData = await getLongLivedToken(
783
- accessCode,
784
- getEnv("NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */),
785
- getEnv("INSTAGRAM_CLIENT_ID" /* INSTAGRAM_CLIENT_ID */),
786
- getEnv("INSTAGRAM_CLIENT_SECRET" /* INSTAGRAM_CLIENT_SECRET */)
787
- );
788
- if (!instagramData?.accessToken)
789
- return handleError2("Unable to reach Instagram");
790
- const personalInfo = await getInstagramUser(instagramData.accessToken);
791
- if (!personalInfo?.username) return handleError2("You are not logged in");
792
- const loggedInUserID = getUserIdFromAccessToken(stateToken);
793
- const errorMessage = await onUser(
794
- personalInfo,
795
- loggedInUserID,
796
- instagramData.accessToken
797
- );
798
- if (errorMessage) return handleError2(errorMessage);
799
- return NextResponse7.redirect(redirectURL);
800
- };
801
- };
802
-
803
- // src/auth/instagram/index.ts
804
- function setupInstagramAuth(props) {
805
- return {
806
- GET: getInstagramRoute(props)
807
- };
808
- }
809
-
810
523
  // src/auth/utils/token.ts
811
524
  import { cookies as cookies4 } from "next/headers";
812
525
  async function getRefreshToken() {
@@ -818,11 +531,8 @@ export {
818
531
  AuthWrapper,
819
532
  checkAuthStatus,
820
533
  getContext,
821
- getInstagramAuthorizationURL,
822
534
  getRefreshToken,
823
- getRefreshedInstagramAccessToken,
824
535
  getTokenizedResponse,
825
536
  setupEmailAuth,
826
- setupGoogleAuth,
827
- setupInstagramAuth
537
+ setupGoogleAuth
828
538
  };