naystack 1.5.8 → 1.5.10

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 -14
  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 -12
  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 -12
  29. package/dist/auth/email/utils.d.mts +41 -2
  30. package/dist/auth/email/utils.d.ts +41 -2
  31. package/dist/auth/email/utils.esm.js +0 -11
  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 -16
  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 -14
  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 +4 -2
@@ -102,7 +102,6 @@ function verifyUser(user, password) {
102
102
 
103
103
  // src/auth/email/utils.ts
104
104
  import { verify as verify2 } from "jsonwebtoken";
105
- import { cookies as cookies2 } from "next/headers";
106
105
  async function massageRequest(req, options) {
107
106
  const data = await req.json();
108
107
  if (!data.password)
@@ -1,15 +1,77 @@
1
1
  import { NextResponse } from 'next/server';
2
2
  import { UserOutput } from '../types.mjs';
3
3
 
4
+ /**
5
+ * Generates a JWT access token for the user (2-hour expiry).
6
+ * @param id - User id to encode in the JWT payload.
7
+ * @param signingKey - Secret used to sign the token (typically `SIGNING_KEY` env var).
8
+ * @returns Signed JWT string.
9
+ * @category Auth
10
+ */
4
11
  declare function generateAccessToken(id: number, signingKey: string): string;
12
+ /**
13
+ * Generates a JWT refresh token for the user (no expiry — persisted in httpOnly cookie).
14
+ * @param id - User id to encode in the JWT payload.
15
+ * @param refreshKey - Secret used to sign the token (typically `REFRESH_KEY` env var).
16
+ * @returns Signed JWT string.
17
+ * @category Auth
18
+ */
5
19
  declare function generateRefreshToken(id: number, refreshKey: string): string;
20
+ /**
21
+ * Builds a NextResponse with access/refresh tokens in the JSON body and sets the refresh cookie.
22
+ *
23
+ * - If `accessToken` is omitted, the refresh cookie is cleared (logout).
24
+ * - If `refreshToken` is empty string, the cookie expires immediately (logout).
25
+ * - If `refreshToken` is a valid string, the cookie is set for 1 year.
26
+ *
27
+ * @param accessToken - Optional access JWT to include in response body.
28
+ * @param refreshToken - Optional refresh JWT to set as httpOnly cookie. Empty string clears the cookie.
29
+ * @returns NextResponse with JSON body and Set-Cookie headers.
30
+ * @category Auth
31
+ */
6
32
  declare function getTokenizedResponse(accessToken?: string, refreshToken?: string): NextResponse<{
7
33
  accessToken: string | undefined;
8
34
  refreshToken: string | undefined;
9
35
  }>;
36
+ /**
37
+ * Decodes a refresh token and returns the user id from the JWT payload.
38
+ * @param refreshToken - JWT refresh token string.
39
+ * @returns User id (number) or `null` if the token is invalid, expired, or missing.
40
+ * @category Auth
41
+ */
10
42
  declare function getUserIdFromRefreshToken(refreshToken?: string): number | null;
43
+ /**
44
+ * Decodes an access token and returns the user id from the JWT payload.
45
+ * @param refreshToken - JWT access token string (parameter name is legacy).
46
+ * @returns User id (number) or `null` if the token is invalid, expired, or missing.
47
+ * @category Auth
48
+ */
11
49
  declare function getUserIdFromAccessToken(refreshToken?: string): number | null;
50
+ /**
51
+ * Verifies a plain password against the user's stored bcrypt hash.
52
+ * @param user - User object with `password` hash.
53
+ * @param password - Plain-text password to verify.
54
+ * @returns `true` if the password matches, `false` otherwise.
55
+ * @category Auth
56
+ */
12
57
  declare function verifyUser(user: UserOutput, password: string): false | Promise<boolean>;
58
+ /**
59
+ * Checks if the current request has a valid refresh cookie. Optionally redirects to a URL if the user is not authorized.
60
+ *
61
+ * Use this in Server Components or layouts to gate access.
62
+ *
63
+ * @param redirectUnauthorizedURL - If set, redirects to this URL when the user is not authorized.
64
+ * @returns `true` if authorized. If not authorized and `redirectUnauthorizedURL` is set, triggers a redirect (never returns).
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * // In a Server Component:
69
+ * import { checkAuthStatus } from "naystack/auth";
70
+ * await checkAuthStatus("/login"); // Redirects to /login if not authenticated
71
+ * ```
72
+ *
73
+ * @category Auth
74
+ */
13
75
  declare function checkAuthStatus(redirectUnauthorizedURL?: string): Promise<boolean>;
14
76
 
15
77
  export { checkAuthStatus, generateAccessToken, generateRefreshToken, getTokenizedResponse, getUserIdFromAccessToken, getUserIdFromRefreshToken, verifyUser };
@@ -1,15 +1,77 @@
1
1
  import { NextResponse } from 'next/server';
2
2
  import { UserOutput } from '../types.js';
3
3
 
4
+ /**
5
+ * Generates a JWT access token for the user (2-hour expiry).
6
+ * @param id - User id to encode in the JWT payload.
7
+ * @param signingKey - Secret used to sign the token (typically `SIGNING_KEY` env var).
8
+ * @returns Signed JWT string.
9
+ * @category Auth
10
+ */
4
11
  declare function generateAccessToken(id: number, signingKey: string): string;
12
+ /**
13
+ * Generates a JWT refresh token for the user (no expiry — persisted in httpOnly cookie).
14
+ * @param id - User id to encode in the JWT payload.
15
+ * @param refreshKey - Secret used to sign the token (typically `REFRESH_KEY` env var).
16
+ * @returns Signed JWT string.
17
+ * @category Auth
18
+ */
5
19
  declare function generateRefreshToken(id: number, refreshKey: string): string;
20
+ /**
21
+ * Builds a NextResponse with access/refresh tokens in the JSON body and sets the refresh cookie.
22
+ *
23
+ * - If `accessToken` is omitted, the refresh cookie is cleared (logout).
24
+ * - If `refreshToken` is empty string, the cookie expires immediately (logout).
25
+ * - If `refreshToken` is a valid string, the cookie is set for 1 year.
26
+ *
27
+ * @param accessToken - Optional access JWT to include in response body.
28
+ * @param refreshToken - Optional refresh JWT to set as httpOnly cookie. Empty string clears the cookie.
29
+ * @returns NextResponse with JSON body and Set-Cookie headers.
30
+ * @category Auth
31
+ */
6
32
  declare function getTokenizedResponse(accessToken?: string, refreshToken?: string): NextResponse<{
7
33
  accessToken: string | undefined;
8
34
  refreshToken: string | undefined;
9
35
  }>;
36
+ /**
37
+ * Decodes a refresh token and returns the user id from the JWT payload.
38
+ * @param refreshToken - JWT refresh token string.
39
+ * @returns User id (number) or `null` if the token is invalid, expired, or missing.
40
+ * @category Auth
41
+ */
10
42
  declare function getUserIdFromRefreshToken(refreshToken?: string): number | null;
43
+ /**
44
+ * Decodes an access token and returns the user id from the JWT payload.
45
+ * @param refreshToken - JWT access token string (parameter name is legacy).
46
+ * @returns User id (number) or `null` if the token is invalid, expired, or missing.
47
+ * @category Auth
48
+ */
11
49
  declare function getUserIdFromAccessToken(refreshToken?: string): number | null;
50
+ /**
51
+ * Verifies a plain password against the user's stored bcrypt hash.
52
+ * @param user - User object with `password` hash.
53
+ * @param password - Plain-text password to verify.
54
+ * @returns `true` if the password matches, `false` otherwise.
55
+ * @category Auth
56
+ */
12
57
  declare function verifyUser(user: UserOutput, password: string): false | Promise<boolean>;
58
+ /**
59
+ * Checks if the current request has a valid refresh cookie. Optionally redirects to a URL if the user is not authorized.
60
+ *
61
+ * Use this in Server Components or layouts to gate access.
62
+ *
63
+ * @param redirectUnauthorizedURL - If set, redirects to this URL when the user is not authorized.
64
+ * @returns `true` if authorized. If not authorized and `redirectUnauthorizedURL` is set, triggers a redirect (never returns).
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * // In a Server Component:
69
+ * import { checkAuthStatus } from "naystack/auth";
70
+ * await checkAuthStatus("/login"); // Redirects to /login if not authenticated
71
+ * ```
72
+ *
73
+ * @category Auth
74
+ */
13
75
  declare function checkAuthStatus(redirectUnauthorizedURL?: string): Promise<boolean>;
14
76
 
15
77
  export { checkAuthStatus, generateAccessToken, generateRefreshToken, getTokenizedResponse, getUserIdFromAccessToken, getUserIdFromRefreshToken, verifyUser };
@@ -1,6 +1,28 @@
1
1
  import { UserOutput, ErrorHandler } from '../types.mjs';
2
2
  import 'next/server';
3
3
 
4
+ /**
5
+ * Options for initializing email auth routes (GET/POST/PUT/DELETE) via `getEmailAuthRoutes`.
6
+ *
7
+ * @property getUser - Fetches user by request data (e.g. `{ email }`); used for login and sign-up duplicate check. Must return a {@link UserOutput} or `undefined`.
8
+ * @property createUser - Creates a new user with the hashed password; returns the created user as {@link UserOutput}.
9
+ * @property onError - Optional custom handler for validation/auth errors; return a NextResponse to override default error responses.
10
+ * @property onSignUp - Optional callback after successful sign-up. Receives `(userId, requestBody)`.
11
+ * @property onLogin - Optional callback after successful login. Receives `(userId, requestBody)`.
12
+ * @property onRefresh - Optional callback when GET refresh is used. Receives `(userId, requestBody)`.
13
+ * @property onLogout - Optional callback when DELETE logout is used. Receives `(userId, requestBody)`.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const options: InitRoutesOptions = {
18
+ * getUser: async ({ email }) => db.query.users.findFirst({ where: eq(users.email, email) }),
19
+ * createUser: async (data) => (await db.insert(users).values(data).returning())[0],
20
+ * onSignUp: async (userId, body) => { console.log("New user:", userId); },
21
+ * };
22
+ * ```
23
+ *
24
+ * @category Auth
25
+ */
4
26
  type InitRoutesOptions = {
5
27
  getUser: (data: any) => Promise<UserOutput | undefined>;
6
28
  createUser: (user: any) => Promise<UserOutput | undefined>;
@@ -1,6 +1,28 @@
1
1
  import { UserOutput, ErrorHandler } from '../types.js';
2
2
  import 'next/server';
3
3
 
4
+ /**
5
+ * Options for initializing email auth routes (GET/POST/PUT/DELETE) via `getEmailAuthRoutes`.
6
+ *
7
+ * @property getUser - Fetches user by request data (e.g. `{ email }`); used for login and sign-up duplicate check. Must return a {@link UserOutput} or `undefined`.
8
+ * @property createUser - Creates a new user with the hashed password; returns the created user as {@link UserOutput}.
9
+ * @property onError - Optional custom handler for validation/auth errors; return a NextResponse to override default error responses.
10
+ * @property onSignUp - Optional callback after successful sign-up. Receives `(userId, requestBody)`.
11
+ * @property onLogin - Optional callback after successful login. Receives `(userId, requestBody)`.
12
+ * @property onRefresh - Optional callback when GET refresh is used. Receives `(userId, requestBody)`.
13
+ * @property onLogout - Optional callback when DELETE logout is used. Receives `(userId, requestBody)`.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const options: InitRoutesOptions = {
18
+ * getUser: async ({ email }) => db.query.users.findFirst({ where: eq(users.email, email) }),
19
+ * createUser: async (data) => (await db.insert(users).values(data).returning())[0],
20
+ * onSignUp: async (userId, body) => { console.log("New user:", userId); },
21
+ * };
22
+ * ```
23
+ *
24
+ * @category Auth
25
+ */
4
26
  type InitRoutesOptions = {
5
27
  getUser: (data: any) => Promise<UserOutput | undefined>;
6
28
  createUser: (user: any) => Promise<UserOutput | undefined>;
@@ -21,13 +21,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var utils_exports = {};
22
22
  __export(utils_exports, {
23
23
  getContext: () => getContext,
24
- logout: () => logout,
25
24
  massageRequest: () => massageRequest,
26
25
  verifyCaptcha: () => verifyCaptcha
27
26
  });
28
27
  module.exports = __toCommonJS(utils_exports);
29
28
  var import_jsonwebtoken2 = require("jsonwebtoken");
30
- var import_headers2 = require("next/headers");
31
29
 
32
30
  // src/auth/email/token.ts
33
31
  var import_bcryptjs = require("bcryptjs");
@@ -177,19 +175,9 @@ var getContext = (req) => {
177
175
  }
178
176
  return { userId: null };
179
177
  };
180
- async function logout(data) {
181
- const Cookie = await (0, import_headers2.cookies)();
182
- Cookie.delete(REFRESH_COOKIE_NAME);
183
- await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
184
- method: "DELETE",
185
- credentials: "include",
186
- body: JSON.stringify(data)
187
- });
188
- }
189
178
  // Annotate the CommonJS export names for ESM import in node:
190
179
  0 && (module.exports = {
191
180
  getContext,
192
- logout,
193
181
  massageRequest,
194
182
  verifyCaptcha
195
183
  });
@@ -3,6 +3,15 @@ import { Context } from '../../graphql/types.mjs';
3
3
  import { InitRoutesOptions } from './types.mjs';
4
4
  import '../types.mjs';
5
5
 
6
+ /**
7
+ * Parses and validates the JSON body for sign-up/login routes: ensures `password` is present and,
8
+ * if `TURNSTILE_KEY` is set, validates the Cloudflare Turnstile captcha.
9
+ *
10
+ * @param req - The NextRequest (body is read via `req.json()`).
11
+ * @param options - Same `InitRoutesOptions` passed to `getEmailAuthRoutes`; used for `onError` when validation fails.
12
+ * @returns Promise of either `{ error: NextResponse }` (validation failed) or `{ data: { password, ...rest } }` with the validated payload.
13
+ * @category Auth
14
+ */
6
15
  declare function massageRequest(req: NextRequest, options: InitRoutesOptions): Promise<{
7
16
  error?: NextResponse;
8
17
  data?: {
@@ -11,8 +20,38 @@ declare function massageRequest(req: NextRequest, options: InitRoutesOptions): P
11
20
  [key: string]: unknown;
12
21
  };
13
22
  }>;
23
+ /**
24
+ * Verifies a Cloudflare Turnstile captcha token via the siteverify API.
25
+ *
26
+ * @param token - The response token from the Turnstile widget on the client.
27
+ * @param secret - Your Turnstile secret key.
28
+ * @returns `true` if verification succeeded, `false` otherwise.
29
+ * @category Auth
30
+ */
14
31
  declare function verifyCaptcha(token: string, secret?: string): Promise<boolean>;
32
+ /**
33
+ * Builds the auth context from a NextRequest: reads either the `Authorization: Bearer <token>` header
34
+ * or the refresh cookie. Use this in REST API routes (outside GraphQL) to identify the current user.
35
+ *
36
+ * The GraphQL server uses this automatically — you typically only need it for custom REST endpoints.
37
+ *
38
+ * @param req - The NextRequest (headers and cookies are read).
39
+ * @returns `Context` with `userId: number | null`. If the user was identified via the refresh cookie
40
+ * (not an access token), `isRefreshID` is set to `true`.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * import { getContext } from "naystack/auth";
45
+ *
46
+ * export const POST = async (req: NextRequest) => {
47
+ * const ctx = getContext(req);
48
+ * if (!ctx?.userId) return new NextResponse("Unauthorized", { status: 401 });
49
+ * // ctx.userId is the authenticated user's id
50
+ * };
51
+ * ```
52
+ *
53
+ * @category Auth
54
+ */
15
55
  declare const getContext: (req: NextRequest) => Context;
16
- declare function logout(data?: object): Promise<void>;
17
56
 
18
- export { getContext, logout, massageRequest, verifyCaptcha };
57
+ export { getContext, massageRequest, verifyCaptcha };
@@ -3,6 +3,15 @@ import { Context } from '../../graphql/types.js';
3
3
  import { InitRoutesOptions } from './types.js';
4
4
  import '../types.js';
5
5
 
6
+ /**
7
+ * Parses and validates the JSON body for sign-up/login routes: ensures `password` is present and,
8
+ * if `TURNSTILE_KEY` is set, validates the Cloudflare Turnstile captcha.
9
+ *
10
+ * @param req - The NextRequest (body is read via `req.json()`).
11
+ * @param options - Same `InitRoutesOptions` passed to `getEmailAuthRoutes`; used for `onError` when validation fails.
12
+ * @returns Promise of either `{ error: NextResponse }` (validation failed) or `{ data: { password, ...rest } }` with the validated payload.
13
+ * @category Auth
14
+ */
6
15
  declare function massageRequest(req: NextRequest, options: InitRoutesOptions): Promise<{
7
16
  error?: NextResponse;
8
17
  data?: {
@@ -11,8 +20,38 @@ declare function massageRequest(req: NextRequest, options: InitRoutesOptions): P
11
20
  [key: string]: unknown;
12
21
  };
13
22
  }>;
23
+ /**
24
+ * Verifies a Cloudflare Turnstile captcha token via the siteverify API.
25
+ *
26
+ * @param token - The response token from the Turnstile widget on the client.
27
+ * @param secret - Your Turnstile secret key.
28
+ * @returns `true` if verification succeeded, `false` otherwise.
29
+ * @category Auth
30
+ */
14
31
  declare function verifyCaptcha(token: string, secret?: string): Promise<boolean>;
32
+ /**
33
+ * Builds the auth context from a NextRequest: reads either the `Authorization: Bearer <token>` header
34
+ * or the refresh cookie. Use this in REST API routes (outside GraphQL) to identify the current user.
35
+ *
36
+ * The GraphQL server uses this automatically — you typically only need it for custom REST endpoints.
37
+ *
38
+ * @param req - The NextRequest (headers and cookies are read).
39
+ * @returns `Context` with `userId: number | null`. If the user was identified via the refresh cookie
40
+ * (not an access token), `isRefreshID` is set to `true`.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * import { getContext } from "naystack/auth";
45
+ *
46
+ * export const POST = async (req: NextRequest) => {
47
+ * const ctx = getContext(req);
48
+ * if (!ctx?.userId) return new NextResponse("Unauthorized", { status: 401 });
49
+ * // ctx.userId is the authenticated user's id
50
+ * };
51
+ * ```
52
+ *
53
+ * @category Auth
54
+ */
15
55
  declare const getContext: (req: NextRequest) => Context;
16
- declare function logout(data?: object): Promise<void>;
17
56
 
18
- export { getContext, logout, massageRequest, verifyCaptcha };
57
+ export { getContext, massageRequest, verifyCaptcha };
@@ -1,6 +1,5 @@
1
1
  // src/auth/email/utils.ts
2
2
  import { verify as verify2 } from "jsonwebtoken";
3
- import { cookies as cookies2 } from "next/headers";
4
3
 
5
4
  // src/auth/email/token.ts
6
5
  import { compare } from "bcryptjs";
@@ -150,18 +149,8 @@ var getContext = (req) => {
150
149
  }
151
150
  return { userId: null };
152
151
  };
153
- async function logout(data) {
154
- const Cookie = await cookies2();
155
- Cookie.delete(REFRESH_COOKIE_NAME);
156
- await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
157
- method: "DELETE",
158
- credentials: "include",
159
- body: JSON.stringify(data)
160
- });
161
- }
162
152
  export {
163
153
  getContext,
164
- logout,
165
154
  massageRequest,
166
155
  verifyCaptcha
167
156
  };
@@ -2,6 +2,11 @@ import { NextRequest, NextResponse } from 'next/server';
2
2
  import { InitGoogleAuthOptions } from './index.mjs';
3
3
  import 'googleapis';
4
4
 
5
+ /**
6
+ * Returns the GET route handler for Google OAuth (initiate and callback).
7
+ * @param options - InitGoogleAuthOptions
8
+ * @returns Async route handler
9
+ */
5
10
  declare const getGoogleGetRoute: ({ getUserIdFromEmail, redirectURL, errorRedirectURL, }: InitGoogleAuthOptions) => (req: NextRequest) => Promise<NextResponse<unknown>>;
6
11
 
7
12
  export { getGoogleGetRoute };
@@ -2,6 +2,11 @@ import { NextRequest, NextResponse } from 'next/server';
2
2
  import { InitGoogleAuthOptions } from './index.js';
3
3
  import 'googleapis';
4
4
 
5
+ /**
6
+ * Returns the GET route handler for Google OAuth (initiate and callback).
7
+ * @param options - InitGoogleAuthOptions
8
+ * @returns Async route handler
9
+ */
5
10
  declare const getGoogleGetRoute: ({ getUserIdFromEmail, redirectURL, errorRedirectURL, }: InitGoogleAuthOptions) => (req: NextRequest) => Promise<NextResponse<unknown>>;
6
11
 
7
12
  export { getGoogleGetRoute };
@@ -1,12 +1,51 @@
1
1
  import * as next_server from 'next/server';
2
2
  import { oauth2_v2 } from 'googleapis';
3
3
 
4
+ /** Google OAuth userinfo schema (from googleapis). */
4
5
  type Schema$Userinfo = oauth2_v2.Schema$Userinfo;
6
+ /**
7
+ * Options for initializing Google OAuth via {@link initGoogleAuth}.
8
+ *
9
+ * @property getUserIdFromEmail - Given Google userinfo (email, name, picture, etc.), resolves to your app's user id. Return `null` if the user should not be authenticated.
10
+ * @property redirectURL - Where to redirect after successful Google auth (e.g. `"/dashboard"`).
11
+ * @property errorRedirectURL - Optional; where to redirect on error. Defaults to `redirectURL` if omitted.
12
+ *
13
+ * @category Auth
14
+ */
5
15
  interface InitGoogleAuthOptions {
6
16
  getUserIdFromEmail: (email: Schema$Userinfo) => Promise<number | null>;
7
17
  redirectURL: string;
8
18
  errorRedirectURL?: string;
9
19
  }
20
+ /**
21
+ * Initializes Google OAuth. Returns a GET handler that initiates the OAuth flow (redirects to Google)
22
+ * and handles the callback (exchanges code for tokens, calls `getUserIdFromEmail`, sets refresh cookie).
23
+ *
24
+ * Mount the GET handler on your Google auth route (e.g. `app/api/(auth)/google/route.ts`).
25
+ *
26
+ * Requires env vars: `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, `NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT`.
27
+ *
28
+ * @param props - Options. See {@link InitGoogleAuthOptions}.
29
+ * @returns Object with `GET` — export as your route's GET handler.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * // app/api/(auth)/google/route.ts
34
+ * import { initGoogleAuth } from "naystack/auth";
35
+ *
36
+ * export const { GET } = initGoogleAuth({
37
+ * getUserIdFromEmail: async (googleUser) => {
38
+ * // Find or create user by Google email
39
+ * const user = await findOrCreateUserByEmail(googleUser.email!);
40
+ * return user?.id ?? null;
41
+ * },
42
+ * redirectURL: "/dashboard",
43
+ * errorRedirectURL: "/login",
44
+ * });
45
+ * ```
46
+ *
47
+ * @category Auth
48
+ */
10
49
  declare function initGoogleAuth(props: InitGoogleAuthOptions): {
11
50
  GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown>>;
12
51
  };
@@ -1,12 +1,51 @@
1
1
  import * as next_server from 'next/server';
2
2
  import { oauth2_v2 } from 'googleapis';
3
3
 
4
+ /** Google OAuth userinfo schema (from googleapis). */
4
5
  type Schema$Userinfo = oauth2_v2.Schema$Userinfo;
6
+ /**
7
+ * Options for initializing Google OAuth via {@link initGoogleAuth}.
8
+ *
9
+ * @property getUserIdFromEmail - Given Google userinfo (email, name, picture, etc.), resolves to your app's user id. Return `null` if the user should not be authenticated.
10
+ * @property redirectURL - Where to redirect after successful Google auth (e.g. `"/dashboard"`).
11
+ * @property errorRedirectURL - Optional; where to redirect on error. Defaults to `redirectURL` if omitted.
12
+ *
13
+ * @category Auth
14
+ */
5
15
  interface InitGoogleAuthOptions {
6
16
  getUserIdFromEmail: (email: Schema$Userinfo) => Promise<number | null>;
7
17
  redirectURL: string;
8
18
  errorRedirectURL?: string;
9
19
  }
20
+ /**
21
+ * Initializes Google OAuth. Returns a GET handler that initiates the OAuth flow (redirects to Google)
22
+ * and handles the callback (exchanges code for tokens, calls `getUserIdFromEmail`, sets refresh cookie).
23
+ *
24
+ * Mount the GET handler on your Google auth route (e.g. `app/api/(auth)/google/route.ts`).
25
+ *
26
+ * Requires env vars: `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, `NEXT_PUBLIC_GOOGLE_AUTH_ENDPOINT`.
27
+ *
28
+ * @param props - Options. See {@link InitGoogleAuthOptions}.
29
+ * @returns Object with `GET` — export as your route's GET handler.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * // app/api/(auth)/google/route.ts
34
+ * import { initGoogleAuth } from "naystack/auth";
35
+ *
36
+ * export const { GET } = initGoogleAuth({
37
+ * getUserIdFromEmail: async (googleUser) => {
38
+ * // Find or create user by Google email
39
+ * const user = await findOrCreateUserByEmail(googleUser.email!);
40
+ * return user?.id ?? null;
41
+ * },
42
+ * redirectURL: "/dashboard",
43
+ * errorRedirectURL: "/login",
44
+ * });
45
+ * ```
46
+ *
47
+ * @category Auth
48
+ */
10
49
  declare function initGoogleAuth(props: InitGoogleAuthOptions): {
11
50
  GET: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown>>;
12
51
  };
@@ -25,8 +25,7 @@ __export(auth_exports, {
25
25
  getEmailAuthRoutes: () => getEmailAuthRoutes,
26
26
  getRefreshToken: () => getRefreshToken,
27
27
  initGoogleAuth: () => initGoogleAuth,
28
- initInstagramAuth: () => initInstagramAuth,
29
- logout: () => logout
28
+ initInstagramAuth: () => initInstagramAuth
30
29
  });
31
30
  module.exports = __toCommonJS(auth_exports);
32
31
 
@@ -157,7 +156,6 @@ async function checkAuthStatus(redirectUnauthorizedURL) {
157
156
 
158
157
  // src/auth/email/utils.ts
159
158
  var import_jsonwebtoken2 = require("jsonwebtoken");
160
- var import_headers2 = require("next/headers");
161
159
 
162
160
  // src/auth/utils/errors.ts
163
161
  var import_server2 = require("next/server");
@@ -234,15 +232,6 @@ var getContext = (req) => {
234
232
  }
235
233
  return { userId: null };
236
234
  };
237
- async function logout(data) {
238
- const Cookie = await (0, import_headers2.cookies)();
239
- Cookie.delete(REFRESH_COOKIE_NAME);
240
- await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
241
- method: "DELETE",
242
- credentials: "include",
243
- body: JSON.stringify(data)
244
- });
245
- }
246
235
 
247
236
  // src/auth/email/routes/delete.ts
248
237
  var getDeleteRoute = (options) => async (req) => {
@@ -526,9 +515,9 @@ function initInstagramAuth(props) {
526
515
  }
527
516
 
528
517
  // src/auth/utils/token.ts
529
- var import_headers3 = require("next/headers");
518
+ var import_headers2 = require("next/headers");
530
519
  async function getRefreshToken() {
531
- const Cookie = await (0, import_headers3.cookies)();
520
+ const Cookie = await (0, import_headers2.cookies)();
532
521
  return Cookie.get(REFRESH_COOKIE_NAME)?.value || null;
533
522
  }
534
523
  // Annotate the CommonJS export names for ESM import in node:
@@ -538,6 +527,5 @@ async function getRefreshToken() {
538
527
  getEmailAuthRoutes,
539
528
  getRefreshToken,
540
529
  initGoogleAuth,
541
- initInstagramAuth,
542
- logout
530
+ initInstagramAuth
543
531
  });
@@ -3,7 +3,7 @@ export { initGoogleAuth } from './google/index.mjs';
3
3
  export { initInstagramAuth } from './instagram/index.mjs';
4
4
  export { getRefreshToken } from './utils/token.mjs';
5
5
  export { checkAuthStatus } from './email/token.mjs';
6
- export { getContext, logout } from './email/utils.mjs';
6
+ export { getContext } from './email/utils.mjs';
7
7
  import 'next/server';
8
8
  import './email/types.mjs';
9
9
  import './types.mjs';
@@ -3,7 +3,7 @@ export { initGoogleAuth } from './google/index.js';
3
3
  export { initInstagramAuth } from './instagram/index.js';
4
4
  export { getRefreshToken } from './utils/token.js';
5
5
  export { checkAuthStatus } from './email/token.js';
6
- export { getContext, logout } from './email/utils.js';
6
+ export { getContext } from './email/utils.js';
7
7
  import 'next/server';
8
8
  import './email/types.js';
9
9
  import './types.js';
@@ -125,7 +125,6 @@ async function checkAuthStatus(redirectUnauthorizedURL) {
125
125
 
126
126
  // src/auth/email/utils.ts
127
127
  import { verify as verify2 } from "jsonwebtoken";
128
- import { cookies as cookies2 } from "next/headers";
129
128
 
130
129
  // src/auth/utils/errors.ts
131
130
  import { NextResponse as NextResponse2 } from "next/server";
@@ -202,15 +201,6 @@ var getContext = (req) => {
202
201
  }
203
202
  return { userId: null };
204
203
  };
205
- async function logout(data) {
206
- const Cookie = await cookies2();
207
- Cookie.delete(REFRESH_COOKIE_NAME);
208
- await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
209
- method: "DELETE",
210
- credentials: "include",
211
- body: JSON.stringify(data)
212
- });
213
- }
214
204
 
215
205
  // src/auth/email/routes/delete.ts
216
206
  var getDeleteRoute = (options) => async (req) => {
@@ -494,9 +484,9 @@ function initInstagramAuth(props) {
494
484
  }
495
485
 
496
486
  // src/auth/utils/token.ts
497
- import { cookies as cookies3 } from "next/headers";
487
+ import { cookies as cookies2 } from "next/headers";
498
488
  async function getRefreshToken() {
499
- const Cookie = await cookies3();
489
+ const Cookie = await cookies2();
500
490
  return Cookie.get(REFRESH_COOKIE_NAME)?.value || null;
501
491
  }
502
492
  export {
@@ -505,6 +495,5 @@ export {
505
495
  getEmailAuthRoutes,
506
496
  getRefreshToken,
507
497
  initGoogleAuth,
508
- initInstagramAuth,
509
- logout
498
+ initInstagramAuth
510
499
  };
@@ -1,3 +1,22 @@
1
+ /**
2
+ * Creates a function that builds the Instagram OAuth authorization URL for a given state token.
3
+ * The state token is typically the user's access token, used to link the Instagram account to the logged-in user.
4
+ *
5
+ * @param redirectURL - The OAuth redirect URI registered with Instagram (e.g. `NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT`).
6
+ * @returns A function `(stateToken: string) => string` that returns the full authorization URL.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { getInstagramAuthorizationURLSetup } from "naystack/auth/instagram/client";
11
+ *
12
+ * const getAuthURL = getInstagramAuthorizationURLSetup("/api/instagram");
13
+ * const url = getAuthURL(userAccessToken);
14
+ * // => "https://www.instagram.com/oauth/authorize?client_id=...&state=...&redirect_uri=..."
15
+ * window.location.href = url;
16
+ * ```
17
+ *
18
+ * @category Auth
19
+ */
1
20
  declare const getInstagramAuthorizationURLSetup: (redirectURL: string) => (token: string) => string;
2
21
 
3
22
  export { getInstagramAuthorizationURLSetup };