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.
- package/README.md +646 -91
- package/dist/auth/constants.d.mts +4 -0
- package/dist/auth/constants.d.ts +4 -0
- package/dist/auth/email/client.d.mts +149 -0
- package/dist/auth/email/client.d.ts +149 -0
- package/dist/auth/email/index.cjs.js +2 -20
- package/dist/auth/email/index.d.mts +41 -1
- package/dist/auth/email/index.d.ts +41 -1
- package/dist/auth/email/index.esm.js +1 -17
- package/dist/auth/email/routes/delete.cjs.js +0 -1
- package/dist/auth/email/routes/delete.d.mts +5 -0
- package/dist/auth/email/routes/delete.d.ts +5 -0
- package/dist/auth/email/routes/delete.esm.js +0 -1
- package/dist/auth/email/routes/get.d.mts +5 -0
- package/dist/auth/email/routes/get.d.ts +5 -0
- package/dist/auth/email/routes/post.cjs.js +0 -1
- package/dist/auth/email/routes/post.d.mts +5 -0
- package/dist/auth/email/routes/post.d.ts +5 -0
- package/dist/auth/email/routes/post.esm.js +0 -1
- package/dist/auth/email/routes/put.cjs.js +0 -1
- package/dist/auth/email/routes/put.d.mts +5 -0
- package/dist/auth/email/routes/put.d.ts +5 -0
- package/dist/auth/email/routes/put.esm.js +0 -1
- package/dist/auth/email/token.d.mts +62 -0
- package/dist/auth/email/token.d.ts +62 -0
- package/dist/auth/email/types.d.mts +22 -0
- package/dist/auth/email/types.d.ts +22 -0
- package/dist/auth/email/utils.cjs.js +0 -18
- package/dist/auth/email/utils.d.mts +41 -3
- package/dist/auth/email/utils.d.ts +41 -3
- package/dist/auth/email/utils.esm.js +0 -16
- package/dist/auth/google/get.d.mts +5 -0
- package/dist/auth/google/get.d.ts +5 -0
- package/dist/auth/google/index.d.mts +39 -0
- package/dist/auth/google/index.d.ts +39 -0
- package/dist/auth/index.cjs.js +4 -22
- package/dist/auth/index.d.mts +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.esm.js +3 -19
- package/dist/auth/instagram/client.d.mts +19 -0
- package/dist/auth/instagram/client.d.ts +19 -0
- package/dist/auth/instagram/index.d.mts +37 -0
- package/dist/auth/instagram/index.d.ts +37 -0
- package/dist/auth/instagram/route.d.mts +5 -0
- package/dist/auth/instagram/route.d.ts +5 -0
- package/dist/auth/instagram/utils.d.mts +13 -0
- package/dist/auth/instagram/utils.d.ts +13 -0
- package/dist/auth/types.d.mts +24 -0
- package/dist/auth/types.d.ts +24 -0
- package/dist/auth/utils/errors.d.mts +10 -0
- package/dist/auth/utils/errors.d.ts +10 -0
- package/dist/auth/utils/token.d.mts +20 -0
- package/dist/auth/utils/token.d.ts +20 -0
- package/dist/client/hooks.d.mts +59 -0
- package/dist/client/hooks.d.ts +59 -0
- package/dist/client/seo.d.mts +46 -0
- package/dist/client/seo.d.ts +46 -0
- package/dist/env.d.mts +61 -0
- package/dist/env.d.ts +61 -0
- package/dist/file/client.d.mts +53 -1
- package/dist/file/client.d.ts +53 -1
- package/dist/file/index.cjs.js +0 -1
- package/dist/file/index.esm.js +0 -1
- package/dist/file/put.cjs.js +0 -1
- package/dist/file/put.d.mts +11 -0
- package/dist/file/put.d.ts +11 -0
- package/dist/file/put.esm.js +0 -1
- package/dist/file/setup.cjs.js +0 -1
- package/dist/file/setup.d.mts +48 -0
- package/dist/file/setup.d.ts +48 -0
- package/dist/file/setup.esm.js +0 -1
- package/dist/file/utils.d.mts +41 -0
- package/dist/file/utils.d.ts +41 -0
- package/dist/graphql/client.d.mts +113 -0
- package/dist/graphql/client.d.ts +113 -0
- package/dist/graphql/errors.d.mts +26 -0
- package/dist/graphql/errors.d.ts +26 -0
- package/dist/graphql/index.cjs.js +2 -3
- package/dist/graphql/index.esm.js +2 -3
- package/dist/graphql/init.cjs.js +0 -1
- package/dist/graphql/init.d.mts +33 -0
- package/dist/graphql/init.d.ts +33 -0
- package/dist/graphql/init.esm.js +0 -1
- package/dist/graphql/server.d.mts +88 -0
- package/dist/graphql/server.d.ts +88 -0
- package/dist/graphql/types.d.mts +21 -0
- package/dist/graphql/types.d.ts +21 -0
- package/dist/graphql/utils.d.mts +217 -0
- package/dist/graphql/utils.d.ts +217 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/socials/instagram/getters.d.mts +115 -0
- package/dist/socials/instagram/getters.d.ts +115 -0
- package/dist/socials/instagram/setters.d.mts +18 -0
- package/dist/socials/instagram/setters.d.ts +18 -0
- package/dist/socials/instagram/types.d.mts +46 -0
- package/dist/socials/instagram/types.d.ts +46 -0
- package/dist/socials/instagram/utils.d.mts +19 -0
- package/dist/socials/instagram/utils.d.ts +19 -0
- package/dist/socials/instagram/webhook.d.mts +31 -0
- package/dist/socials/instagram/webhook.d.ts +31 -0
- package/dist/socials/meta-webhook.d.mts +11 -0
- package/dist/socials/meta-webhook.d.ts +11 -0
- package/dist/socials/threads/getters.d.mts +57 -0
- package/dist/socials/threads/getters.d.ts +57 -0
- package/dist/socials/threads/setters.d.mts +59 -0
- package/dist/socials/threads/setters.d.ts +59 -0
- package/dist/socials/threads/types.d.mts +9 -0
- package/dist/socials/threads/types.d.ts +9 -0
- package/dist/socials/threads/utils.d.mts +19 -0
- package/dist/socials/threads/utils.d.ts +19 -0
- package/dist/socials/threads/webhook.d.mts +30 -0
- package/dist/socials/threads/webhook.d.ts +30 -0
- package/package.json +9 -5
|
@@ -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 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 };
|
|
@@ -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,14 +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
|
-
getCurrentRefreshToken: () => getCurrentRefreshToken,
|
|
25
|
-
logout: () => logout,
|
|
26
24
|
massageRequest: () => massageRequest,
|
|
27
25
|
verifyCaptcha: () => verifyCaptcha
|
|
28
26
|
});
|
|
29
27
|
module.exports = __toCommonJS(utils_exports);
|
|
30
28
|
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
31
|
-
var import_headers2 = require("next/headers");
|
|
32
29
|
|
|
33
30
|
// src/auth/email/token.ts
|
|
34
31
|
var import_bcryptjs = require("bcryptjs");
|
|
@@ -178,24 +175,9 @@ var getContext = (req) => {
|
|
|
178
175
|
}
|
|
179
176
|
return { userId: null };
|
|
180
177
|
};
|
|
181
|
-
async function logout(data) {
|
|
182
|
-
const Cookie = await (0, import_headers2.cookies)();
|
|
183
|
-
Cookie.delete(REFRESH_COOKIE_NAME);
|
|
184
|
-
await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
|
|
185
|
-
method: "DELETE",
|
|
186
|
-
credentials: "include",
|
|
187
|
-
body: JSON.stringify(data)
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
async function getCurrentRefreshToken() {
|
|
191
|
-
const Cookie = await (0, import_headers2.cookies)();
|
|
192
|
-
return Cookie.get(REFRESH_COOKIE_NAME)?.value;
|
|
193
|
-
}
|
|
194
178
|
// Annotate the CommonJS export names for ESM import in node:
|
|
195
179
|
0 && (module.exports = {
|
|
196
180
|
getContext,
|
|
197
|
-
getCurrentRefreshToken,
|
|
198
|
-
logout,
|
|
199
181
|
massageRequest,
|
|
200
182
|
verifyCaptcha
|
|
201
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,9 +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
|
-
declare function getCurrentRefreshToken(): Promise<string | undefined>;
|
|
18
56
|
|
|
19
|
-
export { getContext,
|
|
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,9 +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
|
-
declare function getCurrentRefreshToken(): Promise<string | undefined>;
|
|
18
56
|
|
|
19
|
-
export { getContext,
|
|
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,23 +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
|
-
async function getCurrentRefreshToken() {
|
|
163
|
-
const Cookie = await cookies2();
|
|
164
|
-
return Cookie.get(REFRESH_COOKIE_NAME)?.value;
|
|
165
|
-
}
|
|
166
152
|
export {
|
|
167
153
|
getContext,
|
|
168
|
-
getCurrentRefreshToken,
|
|
169
|
-
logout,
|
|
170
154
|
massageRequest,
|
|
171
155
|
verifyCaptcha
|
|
172
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
|
};
|
package/dist/auth/index.cjs.js
CHANGED
|
@@ -22,12 +22,10 @@ var auth_exports = {};
|
|
|
22
22
|
__export(auth_exports, {
|
|
23
23
|
checkAuthStatus: () => checkAuthStatus,
|
|
24
24
|
getContext: () => getContext,
|
|
25
|
-
getCurrentRefreshToken: () => getCurrentRefreshToken,
|
|
26
25
|
getEmailAuthRoutes: () => getEmailAuthRoutes,
|
|
27
26
|
getRefreshToken: () => getRefreshToken,
|
|
28
27
|
initGoogleAuth: () => initGoogleAuth,
|
|
29
|
-
initInstagramAuth: () => initInstagramAuth
|
|
30
|
-
logout: () => logout
|
|
28
|
+
initInstagramAuth: () => initInstagramAuth
|
|
31
29
|
});
|
|
32
30
|
module.exports = __toCommonJS(auth_exports);
|
|
33
31
|
|
|
@@ -158,7 +156,6 @@ async function checkAuthStatus(redirectUnauthorizedURL) {
|
|
|
158
156
|
|
|
159
157
|
// src/auth/email/utils.ts
|
|
160
158
|
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
161
|
-
var import_headers2 = require("next/headers");
|
|
162
159
|
|
|
163
160
|
// src/auth/utils/errors.ts
|
|
164
161
|
var import_server2 = require("next/server");
|
|
@@ -235,19 +232,6 @@ var getContext = (req) => {
|
|
|
235
232
|
}
|
|
236
233
|
return { userId: null };
|
|
237
234
|
};
|
|
238
|
-
async function logout(data) {
|
|
239
|
-
const Cookie = await (0, import_headers2.cookies)();
|
|
240
|
-
Cookie.delete(REFRESH_COOKIE_NAME);
|
|
241
|
-
await fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
|
|
242
|
-
method: "DELETE",
|
|
243
|
-
credentials: "include",
|
|
244
|
-
body: JSON.stringify(data)
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
async function getCurrentRefreshToken() {
|
|
248
|
-
const Cookie = await (0, import_headers2.cookies)();
|
|
249
|
-
return Cookie.get(REFRESH_COOKIE_NAME)?.value;
|
|
250
|
-
}
|
|
251
235
|
|
|
252
236
|
// src/auth/email/routes/delete.ts
|
|
253
237
|
var getDeleteRoute = (options) => async (req) => {
|
|
@@ -531,19 +515,17 @@ function initInstagramAuth(props) {
|
|
|
531
515
|
}
|
|
532
516
|
|
|
533
517
|
// src/auth/utils/token.ts
|
|
534
|
-
var
|
|
518
|
+
var import_headers2 = require("next/headers");
|
|
535
519
|
async function getRefreshToken() {
|
|
536
|
-
const Cookie = await (0,
|
|
520
|
+
const Cookie = await (0, import_headers2.cookies)();
|
|
537
521
|
return Cookie.get(REFRESH_COOKIE_NAME)?.value || null;
|
|
538
522
|
}
|
|
539
523
|
// Annotate the CommonJS export names for ESM import in node:
|
|
540
524
|
0 && (module.exports = {
|
|
541
525
|
checkAuthStatus,
|
|
542
526
|
getContext,
|
|
543
|
-
getCurrentRefreshToken,
|
|
544
527
|
getEmailAuthRoutes,
|
|
545
528
|
getRefreshToken,
|
|
546
529
|
initGoogleAuth,
|
|
547
|
-
initInstagramAuth
|
|
548
|
-
logout
|
|
530
|
+
initInstagramAuth
|
|
549
531
|
});
|
package/dist/auth/index.d.mts
CHANGED
|
@@ -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
|
|
6
|
+
export { getContext } from './email/utils.mjs';
|
|
7
7
|
import 'next/server';
|
|
8
8
|
import './email/types.mjs';
|
|
9
9
|
import './types.mjs';
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -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
|
|
6
|
+
export { getContext } from './email/utils.js';
|
|
7
7
|
import 'next/server';
|
|
8
8
|
import './email/types.js';
|
|
9
9
|
import './types.js';
|