naystack 1.5.9 → 1.5.11

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 (114) hide show
  1. package/README.md +646 -91
  2. package/dist/auth/constants.d.mts +4 -0
  3. package/dist/auth/constants.d.ts +4 -0
  4. package/dist/auth/email/client.d.mts +149 -0
  5. package/dist/auth/email/client.d.ts +149 -0
  6. package/dist/auth/email/index.cjs.js +2 -20
  7. package/dist/auth/email/index.d.mts +41 -1
  8. package/dist/auth/email/index.d.ts +41 -1
  9. package/dist/auth/email/index.esm.js +1 -17
  10. package/dist/auth/email/routes/delete.cjs.js +0 -1
  11. package/dist/auth/email/routes/delete.d.mts +5 -0
  12. package/dist/auth/email/routes/delete.d.ts +5 -0
  13. package/dist/auth/email/routes/delete.esm.js +0 -1
  14. package/dist/auth/email/routes/get.d.mts +5 -0
  15. package/dist/auth/email/routes/get.d.ts +5 -0
  16. package/dist/auth/email/routes/post.cjs.js +0 -1
  17. package/dist/auth/email/routes/post.d.mts +5 -0
  18. package/dist/auth/email/routes/post.d.ts +5 -0
  19. package/dist/auth/email/routes/post.esm.js +0 -1
  20. package/dist/auth/email/routes/put.cjs.js +0 -1
  21. package/dist/auth/email/routes/put.d.mts +5 -0
  22. package/dist/auth/email/routes/put.d.ts +5 -0
  23. package/dist/auth/email/routes/put.esm.js +0 -1
  24. package/dist/auth/email/token.d.mts +62 -0
  25. package/dist/auth/email/token.d.ts +62 -0
  26. package/dist/auth/email/types.d.mts +22 -0
  27. package/dist/auth/email/types.d.ts +22 -0
  28. package/dist/auth/email/utils.cjs.js +0 -18
  29. package/dist/auth/email/utils.d.mts +41 -3
  30. package/dist/auth/email/utils.d.ts +41 -3
  31. package/dist/auth/email/utils.esm.js +0 -16
  32. package/dist/auth/google/get.d.mts +5 -0
  33. package/dist/auth/google/get.d.ts +5 -0
  34. package/dist/auth/google/index.d.mts +39 -0
  35. package/dist/auth/google/index.d.ts +39 -0
  36. package/dist/auth/index.cjs.js +4 -22
  37. package/dist/auth/index.d.mts +1 -1
  38. package/dist/auth/index.d.ts +1 -1
  39. package/dist/auth/index.esm.js +3 -19
  40. package/dist/auth/instagram/client.d.mts +19 -0
  41. package/dist/auth/instagram/client.d.ts +19 -0
  42. package/dist/auth/instagram/index.d.mts +37 -0
  43. package/dist/auth/instagram/index.d.ts +37 -0
  44. package/dist/auth/instagram/route.d.mts +5 -0
  45. package/dist/auth/instagram/route.d.ts +5 -0
  46. package/dist/auth/instagram/utils.d.mts +13 -0
  47. package/dist/auth/instagram/utils.d.ts +13 -0
  48. package/dist/auth/types.d.mts +24 -0
  49. package/dist/auth/types.d.ts +24 -0
  50. package/dist/auth/utils/errors.d.mts +10 -0
  51. package/dist/auth/utils/errors.d.ts +10 -0
  52. package/dist/auth/utils/token.d.mts +20 -0
  53. package/dist/auth/utils/token.d.ts +20 -0
  54. package/dist/client/hooks.d.mts +59 -0
  55. package/dist/client/hooks.d.ts +59 -0
  56. package/dist/client/seo.d.mts +46 -0
  57. package/dist/client/seo.d.ts +46 -0
  58. package/dist/env.d.mts +61 -0
  59. package/dist/env.d.ts +61 -0
  60. package/dist/file/client.d.mts +53 -1
  61. package/dist/file/client.d.ts +53 -1
  62. package/dist/file/index.cjs.js +0 -1
  63. package/dist/file/index.esm.js +0 -1
  64. package/dist/file/put.cjs.js +0 -1
  65. package/dist/file/put.d.mts +11 -0
  66. package/dist/file/put.d.ts +11 -0
  67. package/dist/file/put.esm.js +0 -1
  68. package/dist/file/setup.cjs.js +0 -1
  69. package/dist/file/setup.d.mts +48 -0
  70. package/dist/file/setup.d.ts +48 -0
  71. package/dist/file/setup.esm.js +0 -1
  72. package/dist/file/utils.d.mts +41 -0
  73. package/dist/file/utils.d.ts +41 -0
  74. package/dist/graphql/client.d.mts +113 -0
  75. package/dist/graphql/client.d.ts +113 -0
  76. package/dist/graphql/errors.d.mts +26 -0
  77. package/dist/graphql/errors.d.ts +26 -0
  78. package/dist/graphql/index.cjs.js +2 -3
  79. package/dist/graphql/index.esm.js +2 -3
  80. package/dist/graphql/init.cjs.js +0 -1
  81. package/dist/graphql/init.d.mts +33 -0
  82. package/dist/graphql/init.d.ts +33 -0
  83. package/dist/graphql/init.esm.js +0 -1
  84. package/dist/graphql/server.d.mts +88 -0
  85. package/dist/graphql/server.d.ts +88 -0
  86. package/dist/graphql/types.d.mts +21 -0
  87. package/dist/graphql/types.d.ts +21 -0
  88. package/dist/graphql/utils.d.mts +217 -0
  89. package/dist/graphql/utils.d.ts +217 -0
  90. package/dist/index.d.mts +16 -0
  91. package/dist/index.d.ts +16 -0
  92. package/dist/socials/instagram/getters.d.mts +115 -0
  93. package/dist/socials/instagram/getters.d.ts +115 -0
  94. package/dist/socials/instagram/setters.d.mts +18 -0
  95. package/dist/socials/instagram/setters.d.ts +18 -0
  96. package/dist/socials/instagram/types.d.mts +46 -0
  97. package/dist/socials/instagram/types.d.ts +46 -0
  98. package/dist/socials/instagram/utils.d.mts +19 -0
  99. package/dist/socials/instagram/utils.d.ts +19 -0
  100. package/dist/socials/instagram/webhook.d.mts +31 -0
  101. package/dist/socials/instagram/webhook.d.ts +31 -0
  102. package/dist/socials/meta-webhook.d.mts +11 -0
  103. package/dist/socials/meta-webhook.d.ts +11 -0
  104. package/dist/socials/threads/getters.d.mts +57 -0
  105. package/dist/socials/threads/getters.d.ts +57 -0
  106. package/dist/socials/threads/setters.d.mts +59 -0
  107. package/dist/socials/threads/setters.d.ts +59 -0
  108. package/dist/socials/threads/types.d.mts +9 -0
  109. package/dist/socials/threads/types.d.ts +9 -0
  110. package/dist/socials/threads/utils.d.mts +19 -0
  111. package/dist/socials/threads/utils.d.ts +19 -0
  112. package/dist/socials/threads/webhook.d.mts +30 -0
  113. package/dist/socials/threads/webhook.d.ts +30 -0
  114. package/package.json +9 -5
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Cookie name used to store the JWT refresh token (httpOnly, 1-year expiry).
3
+ * @category Auth
4
+ */
1
5
  declare const REFRESH_COOKIE_NAME = "refresh";
2
6
 
3
7
  export { REFRESH_COOKIE_NAME };
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Cookie name used to store the JWT refresh token (httpOnly, 1-year expiry).
3
+ * @category Auth
4
+ */
1
5
  declare const REFRESH_COOKIE_NAME = "refresh";
2
6
 
3
7
  export { REFRESH_COOKIE_NAME };
@@ -1,16 +1,165 @@
1
1
  import React__default, { Dispatch, SetStateAction } from 'react';
2
2
 
3
+ /**
4
+ * React context holding the current access token and setter; used by useToken/useSetToken and auth hooks.
5
+ * @category Auth
6
+ */
3
7
  declare const TokenContext: React__default.Context<{
4
8
  token: string | null;
5
9
  setToken: Dispatch<SetStateAction<string | null>>;
6
10
  }>;
11
+ /**
12
+ * Provider that fetches the current access token from your auth endpoint and exposes it via TokenContext.
13
+ * Wrap your app (or the part that needs auth) so that `useToken()`, `useLogin()`, `useSignUp()`, `useLogout()` work.
14
+ * On mount it GETs `NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT` with credentials; the response's `accessToken` is stored and provided to children.
15
+ *
16
+ * Must be placed **above** `ApolloWrapper` in the component tree (since `ApolloWrapper` needs the token).
17
+ *
18
+ * @param children - React children (e.g. your app or a layout).
19
+ * @returns TokenContext.Provider wrapping children.
20
+ *
21
+ * @example
22
+ * ```tsx
23
+ * // app/layout.tsx
24
+ * import { AuthWrapper } from "naystack/auth/email/client";
25
+ * import { ApolloWrapper } from "naystack/graphql/client";
26
+ *
27
+ * export default function RootLayout({ children }: { children: React.ReactNode }) {
28
+ * return (
29
+ * <html lang="en">
30
+ * <body>
31
+ * <AuthWrapper>
32
+ * <ApolloWrapper>{children}</ApolloWrapper>
33
+ * </AuthWrapper>
34
+ * </body>
35
+ * </html>
36
+ * );
37
+ * }
38
+ * ```
39
+ *
40
+ * @category Auth
41
+ */
7
42
  declare const AuthWrapper: ({ children }: {
8
43
  children: React__default.ReactNode;
9
44
  }) => React__default.JSX.Element;
45
+ /**
46
+ * Returns the current JWT access token from TokenContext. Must be used inside `AuthWrapper`.
47
+ *
48
+ * Common uses: conditional rendering based on auth state, passing to custom fetch headers.
49
+ *
50
+ * @returns The access token string, or `null` if not yet loaded or user is logged out.
51
+ *
52
+ * @example
53
+ * ```tsx
54
+ * import { useToken } from "naystack/auth/email/client";
55
+ *
56
+ * function Navbar() {
57
+ * const token = useToken();
58
+ * return <Link href={token ? "/dashboard" : "/login"}>{token ? "Dashboard" : "Log in"}</Link>;
59
+ * }
60
+ * ```
61
+ *
62
+ * @example Using the token for custom API calls:
63
+ * ```tsx
64
+ * const token = useToken();
65
+ * const response = await fetch("/api/custom", {
66
+ * headers: { Authorization: `Bearer ${token}` },
67
+ * });
68
+ * ```
69
+ *
70
+ * @category Auth
71
+ */
10
72
  declare function useToken(): string | null;
73
+ /**
74
+ * Returns the setter for the access token in TokenContext. Use to update token after login/signup or clear it on logout.
75
+ * Must be used inside `AuthWrapper`. Typically you won't need this directly — use `useLogin`, `useSignUp`, and `useLogout` instead.
76
+ *
77
+ * @returns `Dispatch<SetStateAction<string | null>>` — call with a string to set the token, or `null` to clear.
78
+ *
79
+ * @category Auth
80
+ */
11
81
  declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null>>;
82
+ /**
83
+ * Returns a sign-up function that POSTs to the auth endpoint with credentials. On success, the response's
84
+ * `accessToken` is stored and the token context updates automatically.
85
+ *
86
+ * The payload must include at least `email` and `password`. You can include any extra fields (e.g. `name`, `designation`)
87
+ * and they will be forwarded to the `createUser` callback on the server.
88
+ *
89
+ * @returns A function `(data) => Promise<null | string>`. Call with sign-up payload. Returns `null` on success, or the error response text on failure.
90
+ *
91
+ * @example
92
+ * ```tsx
93
+ * import { useSignUp } from "naystack/auth/email/client";
94
+ *
95
+ * function SignUpForm() {
96
+ * const signUp = useSignUp();
97
+ *
98
+ * const handleSubmit = async (data: { name: string; email: string; password: string }) => {
99
+ * const error = await signUp(data);
100
+ * if (error) {
101
+ * setMessage(error); // e.g. "A user already exists"
102
+ * } else {
103
+ * router.replace("/dashboard");
104
+ * }
105
+ * };
106
+ * }
107
+ * ```
108
+ *
109
+ * @category Auth
110
+ */
12
111
  declare function useSignUp(): (data: object) => Promise<string | null>;
112
+ /**
113
+ * Returns a login function that PUTs to the auth endpoint with credentials. On success, the response's
114
+ * `accessToken` is stored and the token context updates automatically.
115
+ *
116
+ * @returns A function `(data) => Promise<null | string>`. Call with `{ email, password }`. Returns `null` on success, or the error message on failure (e.g. `"Invalid password"`).
117
+ *
118
+ * @example
119
+ * ```tsx
120
+ * import { useLogin } from "naystack/auth/email/client";
121
+ *
122
+ * function LoginForm() {
123
+ * const login = useLogin();
124
+ *
125
+ * const handleSubmit = async (data: { email: string; password: string }) => {
126
+ * const error = await login(data);
127
+ * if (error) {
128
+ * form.setError("password", { message: error });
129
+ * } else {
130
+ * router.replace("/dashboard");
131
+ * }
132
+ * };
133
+ * }
134
+ * ```
135
+ *
136
+ * @category Auth
137
+ */
13
138
  declare function useLogin(): (data: object) => Promise<string | null>;
139
+ /**
140
+ * Returns a logout function that clears the token in context and sends DELETE to the auth endpoint with credentials.
141
+ * The token is cleared **immediately** (optimistic), and the DELETE request runs in the background.
142
+ *
143
+ * @returns A function `(data?) => Promise<void>`. Call with optional body (forwarded to the server's `onLogout` callback). Token is cleared immediately.
144
+ *
145
+ * @example
146
+ * ```tsx
147
+ * import { useLogout } from "naystack/auth/email/client";
148
+ *
149
+ * function SettingsPage() {
150
+ * const logout = useLogout();
151
+ * const router = useRouter();
152
+ *
153
+ * return (
154
+ * <button onClick={() => { logout(); router.push("/login"); }}>
155
+ * Log out
156
+ * </button>
157
+ * );
158
+ * }
159
+ * ```
160
+ *
161
+ * @category Auth
162
+ */
14
163
  declare function useLogout(): (data?: object) => Promise<void>;
15
164
 
16
165
  export { AuthWrapper, TokenContext, useLogin, useLogout, useSetToken, useSignUp, useToken };
@@ -1,16 +1,165 @@
1
1
  import React__default, { Dispatch, SetStateAction } from 'react';
2
2
 
3
+ /**
4
+ * React context holding the current access token and setter; used by useToken/useSetToken and auth hooks.
5
+ * @category Auth
6
+ */
3
7
  declare const TokenContext: React__default.Context<{
4
8
  token: string | null;
5
9
  setToken: Dispatch<SetStateAction<string | null>>;
6
10
  }>;
11
+ /**
12
+ * Provider that fetches the current access token from your auth endpoint and exposes it via TokenContext.
13
+ * Wrap your app (or the part that needs auth) so that `useToken()`, `useLogin()`, `useSignUp()`, `useLogout()` work.
14
+ * On mount it GETs `NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT` with credentials; the response's `accessToken` is stored and provided to children.
15
+ *
16
+ * Must be placed **above** `ApolloWrapper` in the component tree (since `ApolloWrapper` needs the token).
17
+ *
18
+ * @param children - React children (e.g. your app or a layout).
19
+ * @returns TokenContext.Provider wrapping children.
20
+ *
21
+ * @example
22
+ * ```tsx
23
+ * // app/layout.tsx
24
+ * import { AuthWrapper } from "naystack/auth/email/client";
25
+ * import { ApolloWrapper } from "naystack/graphql/client";
26
+ *
27
+ * export default function RootLayout({ children }: { children: React.ReactNode }) {
28
+ * return (
29
+ * <html lang="en">
30
+ * <body>
31
+ * <AuthWrapper>
32
+ * <ApolloWrapper>{children}</ApolloWrapper>
33
+ * </AuthWrapper>
34
+ * </body>
35
+ * </html>
36
+ * );
37
+ * }
38
+ * ```
39
+ *
40
+ * @category Auth
41
+ */
7
42
  declare const AuthWrapper: ({ children }: {
8
43
  children: React__default.ReactNode;
9
44
  }) => React__default.JSX.Element;
45
+ /**
46
+ * Returns the current JWT access token from TokenContext. Must be used inside `AuthWrapper`.
47
+ *
48
+ * Common uses: conditional rendering based on auth state, passing to custom fetch headers.
49
+ *
50
+ * @returns The access token string, or `null` if not yet loaded or user is logged out.
51
+ *
52
+ * @example
53
+ * ```tsx
54
+ * import { useToken } from "naystack/auth/email/client";
55
+ *
56
+ * function Navbar() {
57
+ * const token = useToken();
58
+ * return <Link href={token ? "/dashboard" : "/login"}>{token ? "Dashboard" : "Log in"}</Link>;
59
+ * }
60
+ * ```
61
+ *
62
+ * @example Using the token for custom API calls:
63
+ * ```tsx
64
+ * const token = useToken();
65
+ * const response = await fetch("/api/custom", {
66
+ * headers: { Authorization: `Bearer ${token}` },
67
+ * });
68
+ * ```
69
+ *
70
+ * @category Auth
71
+ */
10
72
  declare function useToken(): string | null;
73
+ /**
74
+ * Returns the setter for the access token in TokenContext. Use to update token after login/signup or clear it on logout.
75
+ * Must be used inside `AuthWrapper`. Typically you won't need this directly — use `useLogin`, `useSignUp`, and `useLogout` instead.
76
+ *
77
+ * @returns `Dispatch<SetStateAction<string | null>>` — call with a string to set the token, or `null` to clear.
78
+ *
79
+ * @category Auth
80
+ */
11
81
  declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null>>;
82
+ /**
83
+ * Returns a sign-up function that POSTs to the auth endpoint with credentials. On success, the response's
84
+ * `accessToken` is stored and the token context updates automatically.
85
+ *
86
+ * The payload must include at least `email` and `password`. You can include any extra fields (e.g. `name`, `designation`)
87
+ * and they will be forwarded to the `createUser` callback on the server.
88
+ *
89
+ * @returns A function `(data) => Promise<null | string>`. Call with sign-up payload. Returns `null` on success, or the error response text on failure.
90
+ *
91
+ * @example
92
+ * ```tsx
93
+ * import { useSignUp } from "naystack/auth/email/client";
94
+ *
95
+ * function SignUpForm() {
96
+ * const signUp = useSignUp();
97
+ *
98
+ * const handleSubmit = async (data: { name: string; email: string; password: string }) => {
99
+ * const error = await signUp(data);
100
+ * if (error) {
101
+ * setMessage(error); // e.g. "A user already exists"
102
+ * } else {
103
+ * router.replace("/dashboard");
104
+ * }
105
+ * };
106
+ * }
107
+ * ```
108
+ *
109
+ * @category Auth
110
+ */
12
111
  declare function useSignUp(): (data: object) => Promise<string | null>;
112
+ /**
113
+ * Returns a login function that PUTs to the auth endpoint with credentials. On success, the response's
114
+ * `accessToken` is stored and the token context updates automatically.
115
+ *
116
+ * @returns A function `(data) => Promise<null | string>`. Call with `{ email, password }`. Returns `null` on success, or the error message on failure (e.g. `"Invalid password"`).
117
+ *
118
+ * @example
119
+ * ```tsx
120
+ * import { useLogin } from "naystack/auth/email/client";
121
+ *
122
+ * function LoginForm() {
123
+ * const login = useLogin();
124
+ *
125
+ * const handleSubmit = async (data: { email: string; password: string }) => {
126
+ * const error = await login(data);
127
+ * if (error) {
128
+ * form.setError("password", { message: error });
129
+ * } else {
130
+ * router.replace("/dashboard");
131
+ * }
132
+ * };
133
+ * }
134
+ * ```
135
+ *
136
+ * @category Auth
137
+ */
13
138
  declare function useLogin(): (data: object) => Promise<string | null>;
139
+ /**
140
+ * Returns a logout function that clears the token in context and sends DELETE to the auth endpoint with credentials.
141
+ * The token is cleared **immediately** (optimistic), and the DELETE request runs in the background.
142
+ *
143
+ * @returns A function `(data?) => Promise<void>`. Call with optional body (forwarded to the server's `onLogout` callback). Token is cleared immediately.
144
+ *
145
+ * @example
146
+ * ```tsx
147
+ * import { useLogout } from "naystack/auth/email/client";
148
+ *
149
+ * function SettingsPage() {
150
+ * const logout = useLogout();
151
+ * const router = useRouter();
152
+ *
153
+ * return (
154
+ * <button onClick={() => { logout(); router.push("/login"); }}>
155
+ * Log out
156
+ * </button>
157
+ * );
158
+ * }
159
+ * ```
160
+ *
161
+ * @category Auth
162
+ */
14
163
  declare function useLogout(): (data?: object) => Promise<void>;
15
164
 
16
165
  export { AuthWrapper, TokenContext, useLogin, useLogout, useSetToken, useSignUp, useToken };
@@ -22,9 +22,7 @@ var email_exports = {};
22
22
  __export(email_exports, {
23
23
  checkAuthStatus: () => checkAuthStatus,
24
24
  getContext: () => getContext,
25
- getCurrentRefreshToken: () => getCurrentRefreshToken,
26
- getEmailAuthRoutes: () => getEmailAuthRoutes,
27
- logout: () => logout
25
+ getEmailAuthRoutes: () => getEmailAuthRoutes
28
26
  });
29
27
  module.exports = __toCommonJS(email_exports);
30
28
 
@@ -143,7 +141,6 @@ async function checkAuthStatus(redirectUnauthorizedURL) {
143
141
 
144
142
  // src/auth/email/utils.ts
145
143
  var import_jsonwebtoken2 = require("jsonwebtoken");
146
- var import_headers2 = require("next/headers");
147
144
 
148
145
  // src/auth/utils/errors.ts
149
146
  var import_server2 = require("next/server");
@@ -220,19 +217,6 @@ var getContext = (req) => {
220
217
  }
221
218
  return { userId: null };
222
219
  };
223
- async function logout(data) {
224
- const Cookie = await (0, import_headers2.cookies)();
225
- Cookie.delete(REFRESH_COOKIE_NAME);
226
- await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
227
- method: "DELETE",
228
- credentials: "include",
229
- body: JSON.stringify(data)
230
- });
231
- }
232
- async function getCurrentRefreshToken() {
233
- const Cookie = await (0, import_headers2.cookies)();
234
- return Cookie.get(REFRESH_COOKIE_NAME)?.value;
235
- }
236
220
 
237
221
  // src/auth/email/routes/delete.ts
238
222
  var getDeleteRoute = (options) => async (req) => {
@@ -327,7 +311,5 @@ function getEmailAuthRoutes(options) {
327
311
  0 && (module.exports = {
328
312
  checkAuthStatus,
329
313
  getContext,
330
- getCurrentRefreshToken,
331
- getEmailAuthRoutes,
332
- logout
314
+ getEmailAuthRoutes
333
315
  });
@@ -1,10 +1,50 @@
1
1
  import * as next_server from 'next/server';
2
2
  import { InitRoutesOptions } from './types.mjs';
3
3
  export { checkAuthStatus } from './token.mjs';
4
- export { getContext, getCurrentRefreshToken, logout } from './utils.mjs';
4
+ export { getContext } from './utils.mjs';
5
5
  import '../types.mjs';
6
6
  import '../../graphql/types.mjs';
7
7
 
8
+ /**
9
+ * Returns Next.js route handlers for email auth. Mount them in your auth API route (e.g. `app/api/(auth)/email/route.ts`).
10
+ *
11
+ * The library automatically hashes passwords on sign-up and reads `SIGNING_KEY` / `REFRESH_KEY` from env vars.
12
+ * Optionally supports Cloudflare Turnstile captcha validation when `TURNSTILE_KEY` is set.
13
+ *
14
+ * @param options - Configuration for the auth routes. See {@link InitRoutesOptions}.
15
+ * @param options.getUser - Given request body (e.g. `{ email }`), returns the user from DB or undefined. Used for login and duplicate check on sign-up.
16
+ * @param options.createUser - Given sign-up data with hashed password, inserts the user and returns the created record.
17
+ * @param options.onError - Optional. Called on validation/auth errors; return a NextResponse to customize the error response.
18
+ * @param options.onSignUp - Optional. Called after successful sign-up with `(userId, requestBody)`.
19
+ * @param options.onLogin - Optional. Called after successful login with `(userId, requestBody)`.
20
+ * @param options.onRefresh - Optional. Called when GET is used to refresh tokens with `(userId, requestBody)`.
21
+ * @param options.onLogout - Optional. Called when DELETE is used to logout with `(userId, requestBody)`.
22
+ * @returns Object with `GET` (refresh tokens), `POST` (sign up), `PUT` (login), `DELETE` (logout) — export as your route's handlers.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * // app/api/(auth)/email/route.ts
27
+ * import { getEmailAuthRoutes } from "naystack/auth";
28
+ *
29
+ * export const { GET, POST, PUT, DELETE } = getEmailAuthRoutes({
30
+ * getUser: async ({ email }: { email: string }) => {
31
+ * const [user] = await db.select({ id: UserTable.id, password: UserTable.password })
32
+ * .from(UserTable).where(eq(UserTable.email, email));
33
+ * return user;
34
+ * },
35
+ * createUser: async (data) => {
36
+ * const [user] = await db.insert(UserTable).values(data)
37
+ * .returning({ id: UserTable.id, password: UserTable.password });
38
+ * return user;
39
+ * },
40
+ * onSignUp: async (userId, body) => {
41
+ * // Optional: run logic after sign-up (e.g. create an org)
42
+ * },
43
+ * });
44
+ * ```
45
+ *
46
+ * @category Auth
47
+ */
8
48
  declare function getEmailAuthRoutes(options: InitRoutesOptions): {
9
49
  GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<{
10
50
  accessToken: string | undefined;
@@ -1,10 +1,50 @@
1
1
  import * as next_server from 'next/server';
2
2
  import { InitRoutesOptions } from './types.js';
3
3
  export { checkAuthStatus } from './token.js';
4
- export { getContext, getCurrentRefreshToken, logout } from './utils.js';
4
+ export { getContext } from './utils.js';
5
5
  import '../types.js';
6
6
  import '../../graphql/types.js';
7
7
 
8
+ /**
9
+ * Returns Next.js route handlers for email auth. Mount them in your auth API route (e.g. `app/api/(auth)/email/route.ts`).
10
+ *
11
+ * The library automatically hashes passwords on sign-up and reads `SIGNING_KEY` / `REFRESH_KEY` from env vars.
12
+ * Optionally supports Cloudflare Turnstile captcha validation when `TURNSTILE_KEY` is set.
13
+ *
14
+ * @param options - Configuration for the auth routes. See {@link InitRoutesOptions}.
15
+ * @param options.getUser - Given request body (e.g. `{ email }`), returns the user from DB or undefined. Used for login and duplicate check on sign-up.
16
+ * @param options.createUser - Given sign-up data with hashed password, inserts the user and returns the created record.
17
+ * @param options.onError - Optional. Called on validation/auth errors; return a NextResponse to customize the error response.
18
+ * @param options.onSignUp - Optional. Called after successful sign-up with `(userId, requestBody)`.
19
+ * @param options.onLogin - Optional. Called after successful login with `(userId, requestBody)`.
20
+ * @param options.onRefresh - Optional. Called when GET is used to refresh tokens with `(userId, requestBody)`.
21
+ * @param options.onLogout - Optional. Called when DELETE is used to logout with `(userId, requestBody)`.
22
+ * @returns Object with `GET` (refresh tokens), `POST` (sign up), `PUT` (login), `DELETE` (logout) — export as your route's handlers.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * // app/api/(auth)/email/route.ts
27
+ * import { getEmailAuthRoutes } from "naystack/auth";
28
+ *
29
+ * export const { GET, POST, PUT, DELETE } = getEmailAuthRoutes({
30
+ * getUser: async ({ email }: { email: string }) => {
31
+ * const [user] = await db.select({ id: UserTable.id, password: UserTable.password })
32
+ * .from(UserTable).where(eq(UserTable.email, email));
33
+ * return user;
34
+ * },
35
+ * createUser: async (data) => {
36
+ * const [user] = await db.insert(UserTable).values(data)
37
+ * .returning({ id: UserTable.id, password: UserTable.password });
38
+ * return user;
39
+ * },
40
+ * onSignUp: async (userId, body) => {
41
+ * // Optional: run logic after sign-up (e.g. create an org)
42
+ * },
43
+ * });
44
+ * ```
45
+ *
46
+ * @category Auth
47
+ */
8
48
  declare function getEmailAuthRoutes(options: InitRoutesOptions): {
9
49
  GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<{
10
50
  accessToken: string | undefined;
@@ -113,7 +113,6 @@ async function checkAuthStatus(redirectUnauthorizedURL) {
113
113
 
114
114
  // src/auth/email/utils.ts
115
115
  import { verify as verify2 } from "jsonwebtoken";
116
- import { cookies as cookies2 } from "next/headers";
117
116
 
118
117
  // src/auth/utils/errors.ts
119
118
  import { NextResponse as NextResponse2 } from "next/server";
@@ -190,19 +189,6 @@ var getContext = (req) => {
190
189
  }
191
190
  return { userId: null };
192
191
  };
193
- async function logout(data) {
194
- const Cookie = await cookies2();
195
- Cookie.delete(REFRESH_COOKIE_NAME);
196
- await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
197
- method: "DELETE",
198
- credentials: "include",
199
- body: JSON.stringify(data)
200
- });
201
- }
202
- async function getCurrentRefreshToken() {
203
- const Cookie = await cookies2();
204
- return Cookie.get(REFRESH_COOKIE_NAME)?.value;
205
- }
206
192
 
207
193
  // src/auth/email/routes/delete.ts
208
194
  var getDeleteRoute = (options) => async (req) => {
@@ -296,7 +282,5 @@ function getEmailAuthRoutes(options) {
296
282
  export {
297
283
  checkAuthStatus,
298
284
  getContext,
299
- getCurrentRefreshToken,
300
- getEmailAuthRoutes,
301
- logout
285
+ getEmailAuthRoutes
302
286
  };
@@ -120,7 +120,6 @@ function getUserIdFromRefreshToken(refreshToken) {
120
120
 
121
121
  // src/auth/email/utils.ts
122
122
  var import_jsonwebtoken2 = require("jsonwebtoken");
123
- var import_headers2 = require("next/headers");
124
123
 
125
124
  // src/auth/utils/errors.ts
126
125
  var import_server2 = require("next/server");
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
3
3
  import { InitRoutesOptions } from '../types.mjs';
4
4
  import '../../types.mjs';
5
5
 
6
+ /**
7
+ * Returns the DELETE route handler for logout.
8
+ * @param options - InitRoutesOptions
9
+ * @returns Async route handler
10
+ */
6
11
  declare const getDeleteRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
7
12
  accessToken: string | undefined;
8
13
  refreshToken: string | undefined;
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
3
3
  import { InitRoutesOptions } from '../types.js';
4
4
  import '../../types.js';
5
5
 
6
+ /**
7
+ * Returns the DELETE route handler for logout.
8
+ * @param options - InitRoutesOptions
9
+ * @returns Async route handler
10
+ */
6
11
  declare const getDeleteRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
7
12
  accessToken: string | undefined;
8
13
  refreshToken: string | undefined;
@@ -94,7 +94,6 @@ function getUserIdFromRefreshToken(refreshToken) {
94
94
 
95
95
  // src/auth/email/utils.ts
96
96
  import { verify as verify2 } from "jsonwebtoken";
97
- import { cookies as cookies2 } from "next/headers";
98
97
 
99
98
  // src/auth/utils/errors.ts
100
99
  import { NextResponse as NextResponse2 } from "next/server";
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
3
3
  import { InitRoutesOptions } from '../types.mjs';
4
4
  import '../../types.mjs';
5
5
 
6
+ /**
7
+ * Returns the GET route handler for token refresh (exchange refresh cookie for new tokens).
8
+ * @param options - InitRoutesOptions
9
+ * @returns Async route handler
10
+ */
6
11
  declare const getGetRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
7
12
  accessToken: string | undefined;
8
13
  refreshToken: string | undefined;
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
3
3
  import { InitRoutesOptions } from '../types.js';
4
4
  import '../../types.js';
5
5
 
6
+ /**
7
+ * Returns the GET route handler for token refresh (exchange refresh cookie for new tokens).
8
+ * @param options - InitRoutesOptions
9
+ * @returns Async route handler
10
+ */
6
11
  declare const getGetRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<{
7
12
  accessToken: string | undefined;
8
13
  refreshToken: string | undefined;
@@ -129,7 +129,6 @@ function verifyUser(user, password) {
129
129
 
130
130
  // src/auth/email/utils.ts
131
131
  var import_jsonwebtoken2 = require("jsonwebtoken");
132
- var import_headers2 = require("next/headers");
133
132
  async function massageRequest(req, options) {
134
133
  const data = await req.json();
135
134
  if (!data.password)
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
3
3
  import { InitRoutesOptions } from '../types.mjs';
4
4
  import '../../types.mjs';
5
5
 
6
+ /**
7
+ * Returns the POST route handler for sign up.
8
+ * @param options - InitRoutesOptions
9
+ * @returns Async route handler
10
+ */
6
11
  declare const getPostRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
7
12
 
8
13
  export { getPostRoute };
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
3
3
  import { InitRoutesOptions } from '../types.js';
4
4
  import '../../types.js';
5
5
 
6
+ /**
7
+ * Returns the POST route handler for sign up.
8
+ * @param options - InitRoutesOptions
9
+ * @returns Async route handler
10
+ */
6
11
  declare const getPostRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
7
12
 
8
13
  export { getPostRoute };
@@ -105,7 +105,6 @@ function verifyUser(user, password) {
105
105
 
106
106
  // src/auth/email/utils.ts
107
107
  import { verify as verify2 } from "jsonwebtoken";
108
- import { cookies as cookies2 } from "next/headers";
109
108
  async function massageRequest(req, options) {
110
109
  const data = await req.json();
111
110
  if (!data.password)
@@ -128,7 +128,6 @@ function verifyUser(user, password) {
128
128
 
129
129
  // src/auth/email/utils.ts
130
130
  var import_jsonwebtoken2 = require("jsonwebtoken");
131
- var import_headers2 = require("next/headers");
132
131
  async function massageRequest(req, options) {
133
132
  const data = await req.json();
134
133
  if (!data.password)
@@ -3,6 +3,11 @@ import { NextRequest } from 'next/server';
3
3
  import { InitRoutesOptions } from '../types.mjs';
4
4
  import '../../types.mjs';
5
5
 
6
+ /**
7
+ * Returns the PUT route handler for login.
8
+ * @param options - InitRoutesOptions
9
+ * @returns Async route handler
10
+ */
6
11
  declare const getPutRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
7
12
 
8
13
  export { getPutRoute };