naystack 1.4.16 → 1.4.20

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 (87) hide show
  1. package/dist/auth/email/client.cjs.js +27 -12
  2. package/dist/auth/email/client.esm.js +27 -12
  3. package/dist/auth/email/index.cjs.js +32 -15
  4. package/dist/auth/email/index.esm.js +32 -15
  5. package/dist/auth/email/routes/delete.cjs.js +14 -4
  6. package/dist/auth/email/routes/delete.esm.js +14 -4
  7. package/dist/auth/email/routes/get.cjs.js +12 -2
  8. package/dist/auth/email/routes/get.esm.js +12 -2
  9. package/dist/auth/email/routes/post.cjs.js +17 -6
  10. package/dist/auth/email/routes/post.esm.js +17 -6
  11. package/dist/auth/email/routes/put.cjs.js +12 -4
  12. package/dist/auth/email/routes/put.esm.js +12 -4
  13. package/dist/auth/email/token.cjs.js +9 -2
  14. package/dist/auth/email/token.esm.js +9 -2
  15. package/dist/auth/email/types.d.mts +1 -7
  16. package/dist/auth/email/types.d.ts +1 -7
  17. package/dist/auth/email/utils.cjs.js +16 -5
  18. package/dist/auth/email/utils.d.mts +2 -2
  19. package/dist/auth/email/utils.d.ts +2 -2
  20. package/dist/auth/email/utils.esm.js +16 -5
  21. package/dist/auth/google/get.cjs.js +15 -7
  22. package/dist/auth/google/get.d.mts +1 -3
  23. package/dist/auth/google/get.d.ts +1 -3
  24. package/dist/auth/google/get.esm.js +15 -7
  25. package/dist/auth/google/index.cjs.js +15 -7
  26. package/dist/auth/google/index.d.mts +0 -5
  27. package/dist/auth/google/index.d.ts +0 -5
  28. package/dist/auth/google/index.esm.js +15 -7
  29. package/dist/auth/index.cjs.js +47 -31
  30. package/dist/auth/index.esm.js +47 -31
  31. package/dist/auth/instagram/client.cjs.js +12 -1
  32. package/dist/auth/instagram/client.d.mts +1 -1
  33. package/dist/auth/instagram/client.d.ts +1 -1
  34. package/dist/auth/instagram/client.esm.js +10 -1
  35. package/dist/auth/instagram/index.cjs.js +16 -9
  36. package/dist/auth/instagram/index.d.mts +0 -2
  37. package/dist/auth/instagram/index.d.ts +0 -2
  38. package/dist/auth/instagram/index.esm.js +16 -9
  39. package/dist/auth/instagram/route.cjs.js +16 -9
  40. package/dist/auth/instagram/route.d.mts +1 -1
  41. package/dist/auth/instagram/route.d.ts +1 -1
  42. package/dist/auth/instagram/route.esm.js +16 -9
  43. package/dist/auth/instagram/utils.cjs.js +10 -1
  44. package/dist/auth/instagram/utils.esm.js +8 -1
  45. package/dist/client/index.cjs.js +8 -1
  46. package/dist/client/index.esm.js +8 -1
  47. package/dist/client/seo.cjs.js +10 -1
  48. package/dist/client/seo.esm.js +8 -1
  49. package/dist/env.cjs.js +57 -0
  50. package/dist/env.d.mts +23 -0
  51. package/dist/env.d.ts +23 -0
  52. package/dist/env.esm.js +31 -0
  53. package/dist/file/client.cjs.js +10 -1
  54. package/dist/file/client.esm.js +10 -1
  55. package/dist/file/index.cjs.js +40 -28
  56. package/dist/file/index.d.mts +0 -2
  57. package/dist/file/index.d.ts +0 -2
  58. package/dist/file/index.esm.js +40 -28
  59. package/dist/file/put.cjs.js +23 -11
  60. package/dist/file/put.d.mts +0 -2
  61. package/dist/file/put.d.ts +0 -2
  62. package/dist/file/put.esm.js +23 -11
  63. package/dist/file/setup.cjs.js +40 -28
  64. package/dist/file/setup.d.mts +0 -7
  65. package/dist/file/setup.d.ts +0 -7
  66. package/dist/file/setup.esm.js +40 -28
  67. package/dist/file/utils.cjs.js +28 -17
  68. package/dist/file/utils.d.mts +6 -10
  69. package/dist/file/utils.d.ts +6 -10
  70. package/dist/file/utils.esm.js +28 -17
  71. package/dist/graphql/client.cjs.js +15 -3
  72. package/dist/graphql/client.d.mts +1 -1
  73. package/dist/graphql/client.d.ts +1 -1
  74. package/dist/graphql/client.esm.js +15 -3
  75. package/dist/graphql/index.cjs.js +65 -3
  76. package/dist/graphql/index.d.mts +1 -0
  77. package/dist/graphql/index.d.ts +1 -0
  78. package/dist/graphql/index.esm.js +65 -3
  79. package/dist/graphql/init.cjs.js +11 -2
  80. package/dist/graphql/init.esm.js +11 -2
  81. package/dist/graphql/server.cjs.js +10 -1
  82. package/dist/graphql/server.esm.js +10 -1
  83. package/dist/graphql/utils.cjs.js +54 -1
  84. package/dist/graphql/utils.d.mts +10 -3
  85. package/dist/graphql/utils.d.ts +10 -3
  86. package/dist/graphql/utils.esm.js +54 -1
  87. package/package.json +1 -1
@@ -41,6 +41,15 @@ __export(client_exports, {
41
41
  });
42
42
  module.exports = __toCommonJS(client_exports);
43
43
  var import_react = __toESM(require("react"));
44
+
45
+ // src/env.ts
46
+ function getEnv(key, skipCheck) {
47
+ const value = process.env[key];
48
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
49
+ return value;
50
+ }
51
+
52
+ // src/auth/email/client.tsx
44
53
  var TokenContext = (0, import_react.createContext)({
45
54
  token: null,
46
55
  setToken: () => null
@@ -48,7 +57,7 @@ var TokenContext = (0, import_react.createContext)({
48
57
  var AuthWrapper = ({ children }) => {
49
58
  const [token, setToken] = (0, import_react.useState)(null);
50
59
  (0, import_react.useEffect)(() => {
51
- fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
60
+ fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
52
61
  credentials: "include"
53
62
  }).then((res) => res.json()).then((data) => setToken(data.accessToken));
54
63
  }, []);
@@ -66,11 +75,14 @@ function useSignUp() {
66
75
  const setToken = useSetToken();
67
76
  return (0, import_react.useCallback)(
68
77
  async (data) => {
69
- const res = await fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
70
- method: "POST",
71
- body: JSON.stringify(data),
72
- credentials: "include"
73
- });
78
+ const res = await fetch(
79
+ getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */),
80
+ {
81
+ method: "POST",
82
+ body: JSON.stringify(data),
83
+ credentials: "include"
84
+ }
85
+ );
74
86
  if (res.ok) {
75
87
  const data2 = await res.json();
76
88
  setToken(data2.accessToken);
@@ -85,11 +97,14 @@ function useLogin() {
85
97
  const setToken = useSetToken();
86
98
  return (0, import_react.useCallback)(
87
99
  async (data) => {
88
- const res = await fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
89
- method: "PUT",
90
- body: JSON.stringify(data),
91
- credentials: "include"
92
- });
100
+ const res = await fetch(
101
+ getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */),
102
+ {
103
+ method: "PUT",
104
+ body: JSON.stringify(data),
105
+ credentials: "include"
106
+ }
107
+ );
93
108
  if (res.ok) {
94
109
  const data2 = await res.json();
95
110
  setToken(data2.accessToken);
@@ -105,7 +120,7 @@ function useLogout() {
105
120
  return (0, import_react.useCallback)(
106
121
  async (data) => {
107
122
  setToken(null);
108
- await fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
123
+ await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
109
124
  method: "DELETE",
110
125
  credentials: "include",
111
126
  body: JSON.stringify(data)
@@ -8,6 +8,15 @@ import React, {
8
8
  useEffect,
9
9
  useState
10
10
  } from "react";
11
+
12
+ // src/env.ts
13
+ function getEnv(key, skipCheck) {
14
+ const value = process.env[key];
15
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
16
+ return value;
17
+ }
18
+
19
+ // src/auth/email/client.tsx
11
20
  var TokenContext = createContext({
12
21
  token: null,
13
22
  setToken: () => null
@@ -15,7 +24,7 @@ var TokenContext = createContext({
15
24
  var AuthWrapper = ({ children }) => {
16
25
  const [token, setToken] = useState(null);
17
26
  useEffect(() => {
18
- fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
27
+ fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
19
28
  credentials: "include"
20
29
  }).then((res) => res.json()).then((data) => setToken(data.accessToken));
21
30
  }, []);
@@ -33,11 +42,14 @@ function useSignUp() {
33
42
  const setToken = useSetToken();
34
43
  return useCallback(
35
44
  async (data) => {
36
- const res = await fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
37
- method: "POST",
38
- body: JSON.stringify(data),
39
- credentials: "include"
40
- });
45
+ const res = await fetch(
46
+ getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */),
47
+ {
48
+ method: "POST",
49
+ body: JSON.stringify(data),
50
+ credentials: "include"
51
+ }
52
+ );
41
53
  if (res.ok) {
42
54
  const data2 = await res.json();
43
55
  setToken(data2.accessToken);
@@ -52,11 +64,14 @@ function useLogin() {
52
64
  const setToken = useSetToken();
53
65
  return useCallback(
54
66
  async (data) => {
55
- const res = await fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
56
- method: "PUT",
57
- body: JSON.stringify(data),
58
- credentials: "include"
59
- });
67
+ const res = await fetch(
68
+ getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */),
69
+ {
70
+ method: "PUT",
71
+ body: JSON.stringify(data),
72
+ credentials: "include"
73
+ }
74
+ );
60
75
  if (res.ok) {
61
76
  const data2 = await res.json();
62
77
  setToken(data2.accessToken);
@@ -72,7 +87,7 @@ function useLogout() {
72
87
  return useCallback(
73
88
  async (data) => {
74
89
  setToken(null);
75
- await fetch(process.env.NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT, {
90
+ await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
76
91
  method: "DELETE",
77
92
  credentials: "include",
78
93
  body: JSON.stringify(data)
@@ -35,6 +35,13 @@ var import_headers = require("next/headers");
35
35
  var import_navigation = require("next/navigation");
36
36
  var import_server = require("next/server");
37
37
 
38
+ // src/env.ts
39
+ function getEnv(key, skipCheck) {
40
+ const value = process.env[key];
41
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
42
+ return value;
43
+ }
44
+
38
45
  // src/auth/constants.ts
39
46
  var REFRESH_COOKIE_NAME = "refresh";
40
47
 
@@ -107,12 +114,13 @@ async function massageRequest(req, options) {
107
114
  return {
108
115
  error: handleError(400, "Missing password", options.onError)
109
116
  };
110
- if (options.turnstileKey) {
117
+ const turnstileKey = getEnv("TURNSTILE_KEY" /* TURNSTILE_KEY */, true);
118
+ if (turnstileKey) {
111
119
  if (!data.captchaToken)
112
120
  return {
113
121
  error: handleError(400, "Missing captchaToken", options.onError)
114
122
  };
115
- if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
123
+ if (!await verifyCaptcha(data.captchaToken, turnstileKey))
116
124
  return {
117
125
  error: handleError(400, "Invalid captcha", options.onError)
118
126
  };
@@ -145,17 +153,20 @@ async function verifyCaptcha(token, secret) {
145
153
  }
146
154
  return false;
147
155
  }
148
- var getContext = (keys, req) => {
156
+ var getContext = (req) => {
149
157
  const bearer = req.headers.get("authorization");
150
158
  if (!bearer) {
151
159
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
152
- const userId = getUserIdFromRefreshToken(keys.refresh, refresh);
160
+ const userId = getUserIdFromRefreshToken(
161
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
162
+ refresh
163
+ );
153
164
  if (userId) return { userId, isRefreshID: true };
154
165
  return { userId: null };
155
166
  }
156
167
  const token = bearer.slice(7);
157
168
  try {
158
- const res = (0, import_jsonwebtoken2.verify)(token, keys.signing);
169
+ const res = (0, import_jsonwebtoken2.verify)(token, getEnv("SIGNING_KEY" /* SIGNING_KEY */));
159
170
  if (typeof res === "string") {
160
171
  return { userId: null };
161
172
  }
@@ -170,7 +181,7 @@ var getContext = (keys, req) => {
170
181
  // src/auth/email/routes/delete.ts
171
182
  var getDeleteRoute = (options) => async (req) => {
172
183
  if (options.onLogout) {
173
- const ctx = await getContext(options.keys, req);
184
+ const ctx = await getContext(req);
174
185
  const body = await req.json();
175
186
  await options.onLogout?.(ctx.userId, body);
176
187
  }
@@ -180,14 +191,17 @@ var getDeleteRoute = (options) => async (req) => {
180
191
  // src/auth/email/routes/get.ts
181
192
  var getGetRoute = (options) => async (req) => {
182
193
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
183
- const userID = getUserIdFromRefreshToken(options.keys.refresh, refresh);
194
+ const userID = getUserIdFromRefreshToken(
195
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
196
+ refresh
197
+ );
184
198
  if (userID) {
185
199
  if (options.onRefresh) {
186
200
  const body = await req.json();
187
201
  await options.onRefresh?.(userID, body);
188
202
  }
189
203
  return getTokenizedResponse(
190
- generateAccessToken(userID, options.keys.signing)
204
+ generateAccessToken(userID, getEnv("SIGNING_KEY" /* SIGNING_KEY */))
191
205
  );
192
206
  }
193
207
  return getTokenizedResponse();
@@ -202,8 +216,11 @@ var getPostRoute = (options) => async (req) => {
202
216
  if (existingUser) {
203
217
  if (await verifyUser(existingUser, data.password)) {
204
218
  return getTokenizedResponse(
205
- generateAccessToken(existingUser.id, options.keys.signing),
206
- generateRefreshToken(existingUser.id, options.keys.refresh)
219
+ generateAccessToken(existingUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
220
+ generateRefreshToken(
221
+ existingUser.id,
222
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */)
223
+ )
207
224
  );
208
225
  }
209
226
  return handleError(400, "A user already exists", options.onError);
@@ -218,8 +235,8 @@ var getPostRoute = (options) => async (req) => {
218
235
  await options.onSignUp?.(newUser.id, data);
219
236
  }
220
237
  return getTokenizedResponse(
221
- generateAccessToken(newUser.id, options.keys.signing),
222
- generateRefreshToken(newUser.id, options.keys.refresh)
238
+ generateAccessToken(newUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
239
+ generateRefreshToken(newUser.id, getEnv("REFRESH_KEY" /* REFRESH_KEY */))
223
240
  );
224
241
  }
225
242
  return getTokenizedResponse();
@@ -237,8 +254,8 @@ var getPutRoute = (options) => async (req) => {
237
254
  await options.onLogin?.(user.id, data);
238
255
  }
239
256
  return getTokenizedResponse(
240
- generateAccessToken(user.id, options.keys.signing),
241
- generateRefreshToken(user.id, options.keys.refresh)
257
+ generateAccessToken(user.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
258
+ generateRefreshToken(user.id, getEnv("REFRESH_KEY" /* REFRESH_KEY */))
242
259
  );
243
260
  }
244
261
  return handleError(403, "Invalid password", options.onError);
@@ -251,7 +268,7 @@ function getEmailAuthRoutes(options) {
251
268
  POST: getPostRoute(options),
252
269
  PUT: getPutRoute(options),
253
270
  DELETE: getDeleteRoute(options),
254
- getContext: (req) => getContext(options.keys, req)
271
+ getContext
255
272
  };
256
273
  }
257
274
  // Annotate the CommonJS export names for ESM import in node:
@@ -8,6 +8,13 @@ import { cookies } from "next/headers";
8
8
  import { redirect } from "next/navigation";
9
9
  import { NextResponse } from "next/server";
10
10
 
11
+ // src/env.ts
12
+ function getEnv(key, skipCheck) {
13
+ const value = process.env[key];
14
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
15
+ return value;
16
+ }
17
+
11
18
  // src/auth/constants.ts
12
19
  var REFRESH_COOKIE_NAME = "refresh";
13
20
 
@@ -80,12 +87,13 @@ async function massageRequest(req, options) {
80
87
  return {
81
88
  error: handleError(400, "Missing password", options.onError)
82
89
  };
83
- if (options.turnstileKey) {
90
+ const turnstileKey = getEnv("TURNSTILE_KEY" /* TURNSTILE_KEY */, true);
91
+ if (turnstileKey) {
84
92
  if (!data.captchaToken)
85
93
  return {
86
94
  error: handleError(400, "Missing captchaToken", options.onError)
87
95
  };
88
- if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
96
+ if (!await verifyCaptcha(data.captchaToken, turnstileKey))
89
97
  return {
90
98
  error: handleError(400, "Invalid captcha", options.onError)
91
99
  };
@@ -118,17 +126,20 @@ async function verifyCaptcha(token, secret) {
118
126
  }
119
127
  return false;
120
128
  }
121
- var getContext = (keys, req) => {
129
+ var getContext = (req) => {
122
130
  const bearer = req.headers.get("authorization");
123
131
  if (!bearer) {
124
132
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
125
- const userId = getUserIdFromRefreshToken(keys.refresh, refresh);
133
+ const userId = getUserIdFromRefreshToken(
134
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
135
+ refresh
136
+ );
126
137
  if (userId) return { userId, isRefreshID: true };
127
138
  return { userId: null };
128
139
  }
129
140
  const token = bearer.slice(7);
130
141
  try {
131
- const res = verify2(token, keys.signing);
142
+ const res = verify2(token, getEnv("SIGNING_KEY" /* SIGNING_KEY */));
132
143
  if (typeof res === "string") {
133
144
  return { userId: null };
134
145
  }
@@ -143,7 +154,7 @@ var getContext = (keys, req) => {
143
154
  // src/auth/email/routes/delete.ts
144
155
  var getDeleteRoute = (options) => async (req) => {
145
156
  if (options.onLogout) {
146
- const ctx = await getContext(options.keys, req);
157
+ const ctx = await getContext(req);
147
158
  const body = await req.json();
148
159
  await options.onLogout?.(ctx.userId, body);
149
160
  }
@@ -153,14 +164,17 @@ var getDeleteRoute = (options) => async (req) => {
153
164
  // src/auth/email/routes/get.ts
154
165
  var getGetRoute = (options) => async (req) => {
155
166
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
156
- const userID = getUserIdFromRefreshToken(options.keys.refresh, refresh);
167
+ const userID = getUserIdFromRefreshToken(
168
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
169
+ refresh
170
+ );
157
171
  if (userID) {
158
172
  if (options.onRefresh) {
159
173
  const body = await req.json();
160
174
  await options.onRefresh?.(userID, body);
161
175
  }
162
176
  return getTokenizedResponse(
163
- generateAccessToken(userID, options.keys.signing)
177
+ generateAccessToken(userID, getEnv("SIGNING_KEY" /* SIGNING_KEY */))
164
178
  );
165
179
  }
166
180
  return getTokenizedResponse();
@@ -175,8 +189,11 @@ var getPostRoute = (options) => async (req) => {
175
189
  if (existingUser) {
176
190
  if (await verifyUser(existingUser, data.password)) {
177
191
  return getTokenizedResponse(
178
- generateAccessToken(existingUser.id, options.keys.signing),
179
- generateRefreshToken(existingUser.id, options.keys.refresh)
192
+ generateAccessToken(existingUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
193
+ generateRefreshToken(
194
+ existingUser.id,
195
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */)
196
+ )
180
197
  );
181
198
  }
182
199
  return handleError(400, "A user already exists", options.onError);
@@ -191,8 +208,8 @@ var getPostRoute = (options) => async (req) => {
191
208
  await options.onSignUp?.(newUser.id, data);
192
209
  }
193
210
  return getTokenizedResponse(
194
- generateAccessToken(newUser.id, options.keys.signing),
195
- generateRefreshToken(newUser.id, options.keys.refresh)
211
+ generateAccessToken(newUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
212
+ generateRefreshToken(newUser.id, getEnv("REFRESH_KEY" /* REFRESH_KEY */))
196
213
  );
197
214
  }
198
215
  return getTokenizedResponse();
@@ -210,8 +227,8 @@ var getPutRoute = (options) => async (req) => {
210
227
  await options.onLogin?.(user.id, data);
211
228
  }
212
229
  return getTokenizedResponse(
213
- generateAccessToken(user.id, options.keys.signing),
214
- generateRefreshToken(user.id, options.keys.refresh)
230
+ generateAccessToken(user.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
231
+ generateRefreshToken(user.id, getEnv("REFRESH_KEY" /* REFRESH_KEY */))
215
232
  );
216
233
  }
217
234
  return handleError(403, "Invalid password", options.onError);
@@ -224,7 +241,7 @@ function getEmailAuthRoutes(options) {
224
241
  POST: getPostRoute(options),
225
242
  PUT: getPutRoute(options),
226
243
  DELETE: getDeleteRoute(options),
227
- getContext: (req) => getContext(options.keys, req)
244
+ getContext
228
245
  };
229
246
  }
230
247
  export {
@@ -31,6 +31,13 @@ var import_headers = require("next/headers");
31
31
  var import_navigation = require("next/navigation");
32
32
  var import_server = require("next/server");
33
33
 
34
+ // src/env.ts
35
+ function getEnv(key, skipCheck) {
36
+ const value = process.env[key];
37
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
38
+ return value;
39
+ }
40
+
34
41
  // src/auth/constants.ts
35
42
  var REFRESH_COOKIE_NAME = "refresh";
36
43
 
@@ -76,17 +83,20 @@ var import_jsonwebtoken2 = require("jsonwebtoken");
76
83
  var import_server2 = require("next/server");
77
84
 
78
85
  // src/auth/email/utils.ts
79
- var getContext = (keys, req) => {
86
+ var getContext = (req) => {
80
87
  const bearer = req.headers.get("authorization");
81
88
  if (!bearer) {
82
89
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
83
- const userId = getUserIdFromRefreshToken(keys.refresh, refresh);
90
+ const userId = getUserIdFromRefreshToken(
91
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
92
+ refresh
93
+ );
84
94
  if (userId) return { userId, isRefreshID: true };
85
95
  return { userId: null };
86
96
  }
87
97
  const token = bearer.slice(7);
88
98
  try {
89
- const res = (0, import_jsonwebtoken2.verify)(token, keys.signing);
99
+ const res = (0, import_jsonwebtoken2.verify)(token, getEnv("SIGNING_KEY" /* SIGNING_KEY */));
90
100
  if (typeof res === "string") {
91
101
  return { userId: null };
92
102
  }
@@ -101,7 +111,7 @@ var getContext = (keys, req) => {
101
111
  // src/auth/email/routes/delete.ts
102
112
  var getDeleteRoute = (options) => async (req) => {
103
113
  if (options.onLogout) {
104
- const ctx = await getContext(options.keys, req);
114
+ const ctx = await getContext(req);
105
115
  const body = await req.json();
106
116
  await options.onLogout?.(ctx.userId, body);
107
117
  }
@@ -5,6 +5,13 @@ import { cookies } from "next/headers";
5
5
  import { redirect } from "next/navigation";
6
6
  import { NextResponse } from "next/server";
7
7
 
8
+ // src/env.ts
9
+ function getEnv(key, skipCheck) {
10
+ const value = process.env[key];
11
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
12
+ return value;
13
+ }
14
+
8
15
  // src/auth/constants.ts
9
16
  var REFRESH_COOKIE_NAME = "refresh";
10
17
 
@@ -50,17 +57,20 @@ import { verify as verify2 } from "jsonwebtoken";
50
57
  import { NextResponse as NextResponse2 } from "next/server";
51
58
 
52
59
  // src/auth/email/utils.ts
53
- var getContext = (keys, req) => {
60
+ var getContext = (req) => {
54
61
  const bearer = req.headers.get("authorization");
55
62
  if (!bearer) {
56
63
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
57
- const userId = getUserIdFromRefreshToken(keys.refresh, refresh);
64
+ const userId = getUserIdFromRefreshToken(
65
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
66
+ refresh
67
+ );
58
68
  if (userId) return { userId, isRefreshID: true };
59
69
  return { userId: null };
60
70
  }
61
71
  const token = bearer.slice(7);
62
72
  try {
63
- const res = verify2(token, keys.signing);
73
+ const res = verify2(token, getEnv("SIGNING_KEY" /* SIGNING_KEY */));
64
74
  if (typeof res === "string") {
65
75
  return { userId: null };
66
76
  }
@@ -75,7 +85,7 @@ var getContext = (keys, req) => {
75
85
  // src/auth/email/routes/delete.ts
76
86
  var getDeleteRoute = (options) => async (req) => {
77
87
  if (options.onLogout) {
78
- const ctx = await getContext(options.keys, req);
88
+ const ctx = await getContext(req);
79
89
  const body = await req.json();
80
90
  await options.onLogout?.(ctx.userId, body);
81
91
  }
@@ -24,6 +24,13 @@ __export(get_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(get_exports);
26
26
 
27
+ // src/env.ts
28
+ function getEnv(key, skipCheck) {
29
+ const value = process.env[key];
30
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
31
+ return value;
32
+ }
33
+
27
34
  // src/auth/constants.ts
28
35
  var REFRESH_COOKIE_NAME = "refresh";
29
36
 
@@ -75,14 +82,17 @@ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
75
82
  // src/auth/email/routes/get.ts
76
83
  var getGetRoute = (options) => async (req) => {
77
84
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
78
- const userID = getUserIdFromRefreshToken(options.keys.refresh, refresh);
85
+ const userID = getUserIdFromRefreshToken(
86
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
87
+ refresh
88
+ );
79
89
  if (userID) {
80
90
  if (options.onRefresh) {
81
91
  const body = await req.json();
82
92
  await options.onRefresh?.(userID, body);
83
93
  }
84
94
  return getTokenizedResponse(
85
- generateAccessToken(userID, options.keys.signing)
95
+ generateAccessToken(userID, getEnv("SIGNING_KEY" /* SIGNING_KEY */))
86
96
  );
87
97
  }
88
98
  return getTokenizedResponse();
@@ -1,3 +1,10 @@
1
+ // src/env.ts
2
+ function getEnv(key, skipCheck) {
3
+ const value = process.env[key];
4
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
5
+ return value;
6
+ }
7
+
1
8
  // src/auth/constants.ts
2
9
  var REFRESH_COOKIE_NAME = "refresh";
3
10
 
@@ -49,14 +56,17 @@ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
49
56
  // src/auth/email/routes/get.ts
50
57
  var getGetRoute = (options) => async (req) => {
51
58
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
52
- const userID = getUserIdFromRefreshToken(options.keys.refresh, refresh);
59
+ const userID = getUserIdFromRefreshToken(
60
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */),
61
+ refresh
62
+ );
53
63
  if (userID) {
54
64
  if (options.onRefresh) {
55
65
  const body = await req.json();
56
66
  await options.onRefresh?.(userID, body);
57
67
  }
58
68
  return getTokenizedResponse(
59
- generateAccessToken(userID, options.keys.signing)
69
+ generateAccessToken(userID, getEnv("SIGNING_KEY" /* SIGNING_KEY */))
60
70
  );
61
71
  }
62
72
  return getTokenizedResponse();
@@ -25,6 +25,13 @@ __export(post_exports, {
25
25
  module.exports = __toCommonJS(post_exports);
26
26
  var import_bcryptjs2 = require("bcryptjs");
27
27
 
28
+ // src/env.ts
29
+ function getEnv(key, skipCheck) {
30
+ const value = process.env[key];
31
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
32
+ return value;
33
+ }
34
+
28
35
  // src/auth/utils/errors.ts
29
36
  var import_server = require("next/server");
30
37
  function handleError(status, message, onError) {
@@ -86,12 +93,13 @@ async function massageRequest(req, options) {
86
93
  return {
87
94
  error: handleError(400, "Missing password", options.onError)
88
95
  };
89
- if (options.turnstileKey) {
96
+ const turnstileKey = getEnv("TURNSTILE_KEY" /* TURNSTILE_KEY */, true);
97
+ if (turnstileKey) {
90
98
  if (!data.captchaToken)
91
99
  return {
92
100
  error: handleError(400, "Missing captchaToken", options.onError)
93
101
  };
94
- if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
102
+ if (!await verifyCaptcha(data.captchaToken, turnstileKey))
95
103
  return {
96
104
  error: handleError(400, "Invalid captcha", options.onError)
97
105
  };
@@ -133,8 +141,11 @@ var getPostRoute = (options) => async (req) => {
133
141
  if (existingUser) {
134
142
  if (await verifyUser(existingUser, data.password)) {
135
143
  return getTokenizedResponse(
136
- generateAccessToken(existingUser.id, options.keys.signing),
137
- generateRefreshToken(existingUser.id, options.keys.refresh)
144
+ generateAccessToken(existingUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
145
+ generateRefreshToken(
146
+ existingUser.id,
147
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */)
148
+ )
138
149
  );
139
150
  }
140
151
  return handleError(400, "A user already exists", options.onError);
@@ -149,8 +160,8 @@ var getPostRoute = (options) => async (req) => {
149
160
  await options.onSignUp?.(newUser.id, data);
150
161
  }
151
162
  return getTokenizedResponse(
152
- generateAccessToken(newUser.id, options.keys.signing),
153
- generateRefreshToken(newUser.id, options.keys.refresh)
163
+ generateAccessToken(newUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
164
+ generateRefreshToken(newUser.id, getEnv("REFRESH_KEY" /* REFRESH_KEY */))
154
165
  );
155
166
  }
156
167
  return getTokenizedResponse();
@@ -1,6 +1,13 @@
1
1
  // src/auth/email/routes/post.ts
2
2
  import { hash } from "bcryptjs";
3
3
 
4
+ // src/env.ts
5
+ function getEnv(key, skipCheck) {
6
+ const value = process.env[key];
7
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
8
+ return value;
9
+ }
10
+
4
11
  // src/auth/utils/errors.ts
5
12
  import { NextResponse } from "next/server";
6
13
  function handleError(status, message, onError) {
@@ -62,12 +69,13 @@ async function massageRequest(req, options) {
62
69
  return {
63
70
  error: handleError(400, "Missing password", options.onError)
64
71
  };
65
- if (options.turnstileKey) {
72
+ const turnstileKey = getEnv("TURNSTILE_KEY" /* TURNSTILE_KEY */, true);
73
+ if (turnstileKey) {
66
74
  if (!data.captchaToken)
67
75
  return {
68
76
  error: handleError(400, "Missing captchaToken", options.onError)
69
77
  };
70
- if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
78
+ if (!await verifyCaptcha(data.captchaToken, turnstileKey))
71
79
  return {
72
80
  error: handleError(400, "Invalid captcha", options.onError)
73
81
  };
@@ -109,8 +117,11 @@ var getPostRoute = (options) => async (req) => {
109
117
  if (existingUser) {
110
118
  if (await verifyUser(existingUser, data.password)) {
111
119
  return getTokenizedResponse(
112
- generateAccessToken(existingUser.id, options.keys.signing),
113
- generateRefreshToken(existingUser.id, options.keys.refresh)
120
+ generateAccessToken(existingUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
121
+ generateRefreshToken(
122
+ existingUser.id,
123
+ getEnv("REFRESH_KEY" /* REFRESH_KEY */)
124
+ )
114
125
  );
115
126
  }
116
127
  return handleError(400, "A user already exists", options.onError);
@@ -125,8 +136,8 @@ var getPostRoute = (options) => async (req) => {
125
136
  await options.onSignUp?.(newUser.id, data);
126
137
  }
127
138
  return getTokenizedResponse(
128
- generateAccessToken(newUser.id, options.keys.signing),
129
- generateRefreshToken(newUser.id, options.keys.refresh)
139
+ generateAccessToken(newUser.id, getEnv("SIGNING_KEY" /* SIGNING_KEY */)),
140
+ generateRefreshToken(newUser.id, getEnv("REFRESH_KEY" /* REFRESH_KEY */))
130
141
  );
131
142
  }
132
143
  return getTokenizedResponse();