naystack 1.1.11 → 1.1.12-beta.2
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/dist/auth/email/index.cjs.js +203 -0
- package/dist/auth/email/index.d.mts +18 -0
- package/dist/auth/email/index.d.ts +18 -0
- package/dist/auth/email/index.esm.js +176 -0
- package/dist/auth/email/routes/delete.cjs.js +58 -0
- package/dist/auth/email/routes/delete.d.mts +8 -0
- package/dist/auth/email/routes/delete.d.ts +8 -0
- package/dist/auth/email/routes/delete.esm.js +31 -0
- package/dist/auth/email/routes/get.cjs.js +83 -0
- package/dist/auth/email/routes/get.d.mts +11 -0
- package/dist/auth/email/routes/get.d.ts +11 -0
- package/dist/auth/email/routes/get.esm.js +56 -0
- package/dist/auth/email/routes/post.cjs.js +149 -0
- package/dist/auth/email/routes/post.d.mts +8 -0
- package/dist/auth/email/routes/post.d.ts +8 -0
- package/dist/auth/email/routes/post.esm.js +124 -0
- package/dist/auth/email/routes/put.cjs.js +135 -0
- package/dist/auth/email/routes/put.d.mts +8 -0
- package/dist/auth/email/routes/put.d.ts +8 -0
- package/dist/auth/email/routes/put.esm.js +108 -0
- package/dist/auth/email/token.cjs.js +85 -0
- package/dist/auth/email/token.d.mts +13 -0
- package/dist/auth/email/token.d.ts +13 -0
- package/dist/auth/email/token.esm.js +56 -0
- package/dist/auth/email/types.cjs.js +18 -0
- package/dist/auth/email/types.d.mts +14 -0
- package/dist/auth/email/types.d.ts +14 -0
- package/dist/auth/email/types.esm.js +0 -0
- package/dist/auth/email/utils.cjs.js +83 -0
- package/dist/auth/email/utils.d.mts +16 -0
- package/dist/auth/email/utils.d.ts +16 -0
- package/dist/auth/email/utils.esm.js +55 -0
- package/dist/auth/index.d.mts +4 -45
- package/dist/auth/index.d.ts +4 -45
- package/dist/auth/types.cjs.js +18 -0
- package/dist/auth/types.d.mts +21 -0
- package/dist/auth/types.d.ts +21 -0
- package/dist/auth/types.esm.js +0 -0
- package/dist/auth/utils/errors.cjs.js +35 -0
- package/dist/auth/utils/errors.d.mts +6 -0
- package/dist/auth/utils/errors.d.ts +6 -0
- package/dist/auth/utils/errors.esm.js +10 -0
- package/dist/graphql/context.cjs.js +76 -0
- package/dist/graphql/context.d.mts +6 -0
- package/dist/graphql/context.d.ts +6 -0
- package/dist/graphql/context.esm.js +51 -0
- package/dist/graphql/index.cjs.js +4 -2
- package/dist/graphql/index.d.mts +8 -23
- package/dist/graphql/index.d.ts +8 -23
- package/dist/graphql/index.esm.js +4 -2
- package/dist/graphql/init.cjs.js +126 -0
- package/dist/graphql/init.d.mts +18 -0
- package/dist/graphql/init.d.ts +18 -0
- package/dist/graphql/init.esm.js +106 -0
- package/dist/graphql/types.cjs.js +18 -0
- package/dist/graphql/types.d.mts +8 -0
- package/dist/graphql/types.d.ts +8 -0
- package/dist/graphql/types.esm.js +0 -0
- package/dist/graphql/utils.cjs.js +134 -0
- package/dist/graphql/utils.d.mts +29 -0
- package/dist/graphql/utils.d.ts +29 -0
- package/dist/graphql/utils.esm.js +118 -0
- package/dist/socials/index.d.mts +9 -130
- package/dist/socials/index.d.ts +9 -130
- package/dist/socials/instagram/getters.cjs.js +119 -0
- package/dist/socials/instagram/getters.d.mts +39 -0
- package/dist/socials/instagram/getters.d.ts +39 -0
- package/dist/socials/instagram/getters.esm.js +86 -0
- package/dist/socials/instagram/setters.cjs.js +54 -0
- package/dist/socials/instagram/setters.d.mts +8 -0
- package/dist/socials/instagram/setters.d.ts +8 -0
- package/dist/socials/instagram/setters.esm.js +27 -0
- package/dist/socials/instagram/types.cjs.js +18 -0
- package/dist/socials/instagram/types.d.mts +57 -0
- package/dist/socials/instagram/types.d.ts +57 -0
- package/dist/socials/instagram/types.esm.js +0 -0
- package/dist/socials/instagram/utils.cjs.js +44 -0
- package/dist/socials/instagram/utils.d.mts +6 -0
- package/dist/socials/instagram/utils.d.ts +6 -0
- package/dist/socials/instagram/utils.esm.js +18 -0
- package/dist/socials/instagram/webhook.cjs.js +57 -0
- package/dist/socials/instagram/webhook.d.mts +12 -0
- package/dist/socials/instagram/webhook.d.ts +12 -0
- package/dist/socials/instagram/webhook.esm.js +30 -0
- package/dist/socials/meta-webhook.cjs.js +36 -0
- package/dist/socials/meta-webhook.d.mts +5 -0
- package/dist/socials/meta-webhook.d.ts +5 -0
- package/dist/socials/meta-webhook.esm.js +11 -0
- package/dist/socials/threads/getters.cjs.js +60 -0
- package/dist/socials/threads/getters.d.mts +7 -0
- package/dist/socials/threads/getters.d.ts +7 -0
- package/dist/socials/threads/getters.esm.js +31 -0
- package/dist/socials/threads/setters.cjs.js +87 -0
- package/dist/socials/threads/setters.d.mts +6 -0
- package/dist/socials/threads/setters.d.ts +6 -0
- package/dist/socials/threads/setters.esm.js +57 -0
- package/dist/socials/threads/types.cjs.js +18 -0
- package/dist/socials/threads/types.d.mts +7 -0
- package/dist/socials/threads/types.d.ts +7 -0
- package/dist/socials/threads/types.esm.js +0 -0
- package/dist/socials/threads/utils.cjs.js +39 -0
- package/dist/socials/threads/utils.d.mts +4 -0
- package/dist/socials/threads/utils.d.ts +4 -0
- package/dist/socials/threads/utils.esm.js +13 -0
- package/dist/socials/threads/webhook.cjs.js +54 -0
- package/dist/socials/threads/webhook.d.mts +12 -0
- package/dist/socials/threads/webhook.d.ts +12 -0
- package/dist/socials/threads/webhook.esm.js +27 -0
- package/package.json +1 -1
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/auth/email/index.ts
|
|
21
|
+
var email_exports = {};
|
|
22
|
+
__export(email_exports, {
|
|
23
|
+
getEmailAuthRoutes: () => getEmailAuthRoutes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(email_exports);
|
|
26
|
+
|
|
27
|
+
// src/auth/email/token.ts
|
|
28
|
+
var import_bcryptjs = require("bcryptjs");
|
|
29
|
+
var import_jsonwebtoken = require("jsonwebtoken");
|
|
30
|
+
var import_server = require("next/server");
|
|
31
|
+
function generateAccessToken(id, signingKey) {
|
|
32
|
+
return (0, import_jsonwebtoken.sign)({ id }, signingKey, {
|
|
33
|
+
expiresIn: "2h"
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function generateRefreshToken(id, refreshKey) {
|
|
37
|
+
return (0, import_jsonwebtoken.sign)({ id }, refreshKey);
|
|
38
|
+
}
|
|
39
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
40
|
+
const body = { accessToken, refreshToken };
|
|
41
|
+
const response = import_server.NextResponse.json(body, {
|
|
42
|
+
status: 200
|
|
43
|
+
});
|
|
44
|
+
if (!accessToken) {
|
|
45
|
+
response.cookies.set("refresh", "", {
|
|
46
|
+
secure: false,
|
|
47
|
+
httpOnly: true,
|
|
48
|
+
expires: 0
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (refreshToken !== void 0) {
|
|
52
|
+
response.cookies.set("refresh", refreshToken, {
|
|
53
|
+
secure: false,
|
|
54
|
+
httpOnly: true,
|
|
55
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return response;
|
|
59
|
+
}
|
|
60
|
+
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
61
|
+
if (refreshToken)
|
|
62
|
+
try {
|
|
63
|
+
const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
|
|
64
|
+
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
65
|
+
return decoded.id;
|
|
66
|
+
} catch (e) {
|
|
67
|
+
if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
function verifyUser(user, password) {
|
|
73
|
+
if (!user.password) return false;
|
|
74
|
+
return (0, import_bcryptjs.compare)(password, user.password);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/auth/email/routes/delete.ts
|
|
78
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
79
|
+
|
|
80
|
+
// src/auth/email/routes/get.ts
|
|
81
|
+
var getGetRoute = (options) => (req) => {
|
|
82
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
83
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
84
|
+
if (userID)
|
|
85
|
+
return getTokenizedResponse(
|
|
86
|
+
generateAccessToken(userID, options.signingKey)
|
|
87
|
+
);
|
|
88
|
+
return getTokenizedResponse();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/auth/email/routes/post.ts
|
|
92
|
+
var import_bcryptjs2 = require("bcryptjs");
|
|
93
|
+
|
|
94
|
+
// src/auth/utils/errors.ts
|
|
95
|
+
var import_server2 = require("next/server");
|
|
96
|
+
function handleError(status, message, onError) {
|
|
97
|
+
const res = onError?.({ status, message });
|
|
98
|
+
if (res) return res;
|
|
99
|
+
return new import_server2.NextResponse(message, { status });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/auth/email/utils.ts
|
|
103
|
+
async function massageRequest(req, options) {
|
|
104
|
+
const data = await req.json();
|
|
105
|
+
if (!data.email || !data.password)
|
|
106
|
+
return {
|
|
107
|
+
error: handleError(400, "Missing email or password", options.onError)
|
|
108
|
+
};
|
|
109
|
+
if (options.turnstileKey) {
|
|
110
|
+
if (!data.captchaToken)
|
|
111
|
+
return { error: handleError(400, "Missing captcha", options.onError) };
|
|
112
|
+
if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
|
|
113
|
+
return {
|
|
114
|
+
error: handleError(400, "Invalid captcha", options.onError)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
data: {
|
|
119
|
+
email: data.email,
|
|
120
|
+
password: data.password,
|
|
121
|
+
...data
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async function verifyCaptcha(token, secret) {
|
|
126
|
+
const res = await fetch(
|
|
127
|
+
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
128
|
+
{
|
|
129
|
+
method: "POST",
|
|
130
|
+
headers: {
|
|
131
|
+
"Content-Type": "application/json"
|
|
132
|
+
},
|
|
133
|
+
body: JSON.stringify({
|
|
134
|
+
secret,
|
|
135
|
+
response: token
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
if (res.ok) {
|
|
140
|
+
const data = await res.json();
|
|
141
|
+
return data.success;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// src/auth/email/routes/post.ts
|
|
147
|
+
var getPostRoute = (options) => async (req) => {
|
|
148
|
+
const { data, error } = await massageRequest(req, options);
|
|
149
|
+
if (error || !data) return error;
|
|
150
|
+
const existingUser = await options.getUser(data.email);
|
|
151
|
+
if (existingUser) {
|
|
152
|
+
if (await verifyUser(existingUser, data.password)) {
|
|
153
|
+
return getTokenizedResponse(
|
|
154
|
+
generateAccessToken(existingUser.id, options.signingKey),
|
|
155
|
+
generateRefreshToken(existingUser.id, options.refreshKey)
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return handleError(400, "A user already exists", options.onError);
|
|
159
|
+
}
|
|
160
|
+
const encryptedPassword = await (0, import_bcryptjs2.hash)(data.password, 10);
|
|
161
|
+
const newUser = await options.createUser({
|
|
162
|
+
...data,
|
|
163
|
+
password: encryptedPassword
|
|
164
|
+
});
|
|
165
|
+
if (newUser) {
|
|
166
|
+
options.onSignUp?.(newUser);
|
|
167
|
+
return getTokenizedResponse(
|
|
168
|
+
generateAccessToken(newUser.id, options.signingKey),
|
|
169
|
+
generateRefreshToken(newUser.id, options.refreshKey)
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return getTokenizedResponse();
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// src/auth/email/routes/put.ts
|
|
176
|
+
var getPutRoute = (options) => async (req) => {
|
|
177
|
+
const { data, error } = await massageRequest(req, options);
|
|
178
|
+
if (error || !data) return error;
|
|
179
|
+
const user = await options.getUser(data.email);
|
|
180
|
+
if (!user)
|
|
181
|
+
return handleError(400, "A user does not exist", options.onError);
|
|
182
|
+
if (await verifyUser(user, data.password)) {
|
|
183
|
+
return getTokenizedResponse(
|
|
184
|
+
generateAccessToken(user.id, options.signingKey),
|
|
185
|
+
generateRefreshToken(user.id, options.refreshKey)
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return handleError(403, "Invalid password", options.onError);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// src/auth/email/index.ts
|
|
192
|
+
function getEmailAuthRoutes(options) {
|
|
193
|
+
return {
|
|
194
|
+
GET: getGetRoute(options),
|
|
195
|
+
POST: getPostRoute(options),
|
|
196
|
+
PUT: getPutRoute(options),
|
|
197
|
+
DELETE: getDeleteRoute()
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
201
|
+
0 && (module.exports = {
|
|
202
|
+
getEmailAuthRoutes
|
|
203
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as next_server from 'next/server';
|
|
2
|
+
import { InitRoutesOptions } from './types.mjs';
|
|
3
|
+
import '../types.mjs';
|
|
4
|
+
|
|
5
|
+
declare function getEmailAuthRoutes(options: InitRoutesOptions): {
|
|
6
|
+
GET: (req: next_server.NextRequest) => next_server.NextResponse<{
|
|
7
|
+
accessToken: string | undefined;
|
|
8
|
+
refreshToken: string | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
POST: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
11
|
+
PUT: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
12
|
+
DELETE: () => next_server.NextResponse<{
|
|
13
|
+
accessToken: string | undefined;
|
|
14
|
+
refreshToken: string | undefined;
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { getEmailAuthRoutes };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as next_server from 'next/server';
|
|
2
|
+
import { InitRoutesOptions } from './types.js';
|
|
3
|
+
import '../types.js';
|
|
4
|
+
|
|
5
|
+
declare function getEmailAuthRoutes(options: InitRoutesOptions): {
|
|
6
|
+
GET: (req: next_server.NextRequest) => next_server.NextResponse<{
|
|
7
|
+
accessToken: string | undefined;
|
|
8
|
+
refreshToken: string | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
POST: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
11
|
+
PUT: (req: next_server.NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
12
|
+
DELETE: () => next_server.NextResponse<{
|
|
13
|
+
accessToken: string | undefined;
|
|
14
|
+
refreshToken: string | undefined;
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { getEmailAuthRoutes };
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// src/auth/email/token.ts
|
|
2
|
+
import { compare } from "bcryptjs";
|
|
3
|
+
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
4
|
+
import { NextResponse } from "next/server";
|
|
5
|
+
function generateAccessToken(id, signingKey) {
|
|
6
|
+
return sign({ id }, signingKey, {
|
|
7
|
+
expiresIn: "2h"
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function generateRefreshToken(id, refreshKey) {
|
|
11
|
+
return sign({ id }, refreshKey);
|
|
12
|
+
}
|
|
13
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
14
|
+
const body = { accessToken, refreshToken };
|
|
15
|
+
const response = NextResponse.json(body, {
|
|
16
|
+
status: 200
|
|
17
|
+
});
|
|
18
|
+
if (!accessToken) {
|
|
19
|
+
response.cookies.set("refresh", "", {
|
|
20
|
+
secure: false,
|
|
21
|
+
httpOnly: true,
|
|
22
|
+
expires: 0
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (refreshToken !== void 0) {
|
|
26
|
+
response.cookies.set("refresh", refreshToken, {
|
|
27
|
+
secure: false,
|
|
28
|
+
httpOnly: true,
|
|
29
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
}
|
|
34
|
+
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
35
|
+
if (refreshToken)
|
|
36
|
+
try {
|
|
37
|
+
const decoded = verify(refreshToken, refreshKey);
|
|
38
|
+
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
39
|
+
return decoded.id;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
function verifyUser(user, password) {
|
|
47
|
+
if (!user.password) return false;
|
|
48
|
+
return compare(password, user.password);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/auth/email/routes/delete.ts
|
|
52
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
53
|
+
|
|
54
|
+
// src/auth/email/routes/get.ts
|
|
55
|
+
var getGetRoute = (options) => (req) => {
|
|
56
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
57
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
58
|
+
if (userID)
|
|
59
|
+
return getTokenizedResponse(
|
|
60
|
+
generateAccessToken(userID, options.signingKey)
|
|
61
|
+
);
|
|
62
|
+
return getTokenizedResponse();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/auth/email/routes/post.ts
|
|
66
|
+
import { hash } from "bcryptjs";
|
|
67
|
+
|
|
68
|
+
// src/auth/utils/errors.ts
|
|
69
|
+
import { NextResponse as NextResponse2 } from "next/server";
|
|
70
|
+
function handleError(status, message, onError) {
|
|
71
|
+
const res = onError?.({ status, message });
|
|
72
|
+
if (res) return res;
|
|
73
|
+
return new NextResponse2(message, { status });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/auth/email/utils.ts
|
|
77
|
+
async function massageRequest(req, options) {
|
|
78
|
+
const data = await req.json();
|
|
79
|
+
if (!data.email || !data.password)
|
|
80
|
+
return {
|
|
81
|
+
error: handleError(400, "Missing email or password", options.onError)
|
|
82
|
+
};
|
|
83
|
+
if (options.turnstileKey) {
|
|
84
|
+
if (!data.captchaToken)
|
|
85
|
+
return { error: handleError(400, "Missing captcha", options.onError) };
|
|
86
|
+
if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
|
|
87
|
+
return {
|
|
88
|
+
error: handleError(400, "Invalid captcha", options.onError)
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
data: {
|
|
93
|
+
email: data.email,
|
|
94
|
+
password: data.password,
|
|
95
|
+
...data
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function verifyCaptcha(token, secret) {
|
|
100
|
+
const res = await fetch(
|
|
101
|
+
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
102
|
+
{
|
|
103
|
+
method: "POST",
|
|
104
|
+
headers: {
|
|
105
|
+
"Content-Type": "application/json"
|
|
106
|
+
},
|
|
107
|
+
body: JSON.stringify({
|
|
108
|
+
secret,
|
|
109
|
+
response: token
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
if (res.ok) {
|
|
114
|
+
const data = await res.json();
|
|
115
|
+
return data.success;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/auth/email/routes/post.ts
|
|
121
|
+
var getPostRoute = (options) => async (req) => {
|
|
122
|
+
const { data, error } = await massageRequest(req, options);
|
|
123
|
+
if (error || !data) return error;
|
|
124
|
+
const existingUser = await options.getUser(data.email);
|
|
125
|
+
if (existingUser) {
|
|
126
|
+
if (await verifyUser(existingUser, data.password)) {
|
|
127
|
+
return getTokenizedResponse(
|
|
128
|
+
generateAccessToken(existingUser.id, options.signingKey),
|
|
129
|
+
generateRefreshToken(existingUser.id, options.refreshKey)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return handleError(400, "A user already exists", options.onError);
|
|
133
|
+
}
|
|
134
|
+
const encryptedPassword = await hash(data.password, 10);
|
|
135
|
+
const newUser = await options.createUser({
|
|
136
|
+
...data,
|
|
137
|
+
password: encryptedPassword
|
|
138
|
+
});
|
|
139
|
+
if (newUser) {
|
|
140
|
+
options.onSignUp?.(newUser);
|
|
141
|
+
return getTokenizedResponse(
|
|
142
|
+
generateAccessToken(newUser.id, options.signingKey),
|
|
143
|
+
generateRefreshToken(newUser.id, options.refreshKey)
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
return getTokenizedResponse();
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// src/auth/email/routes/put.ts
|
|
150
|
+
var getPutRoute = (options) => async (req) => {
|
|
151
|
+
const { data, error } = await massageRequest(req, options);
|
|
152
|
+
if (error || !data) return error;
|
|
153
|
+
const user = await options.getUser(data.email);
|
|
154
|
+
if (!user)
|
|
155
|
+
return handleError(400, "A user does not exist", options.onError);
|
|
156
|
+
if (await verifyUser(user, data.password)) {
|
|
157
|
+
return getTokenizedResponse(
|
|
158
|
+
generateAccessToken(user.id, options.signingKey),
|
|
159
|
+
generateRefreshToken(user.id, options.refreshKey)
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
return handleError(403, "Invalid password", options.onError);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/auth/email/index.ts
|
|
166
|
+
function getEmailAuthRoutes(options) {
|
|
167
|
+
return {
|
|
168
|
+
GET: getGetRoute(options),
|
|
169
|
+
POST: getPostRoute(options),
|
|
170
|
+
PUT: getPutRoute(options),
|
|
171
|
+
DELETE: getDeleteRoute()
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
export {
|
|
175
|
+
getEmailAuthRoutes
|
|
176
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/auth/email/routes/delete.ts
|
|
21
|
+
var delete_exports = {};
|
|
22
|
+
__export(delete_exports, {
|
|
23
|
+
getDeleteRoute: () => getDeleteRoute
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(delete_exports);
|
|
26
|
+
|
|
27
|
+
// src/auth/email/token.ts
|
|
28
|
+
var import_bcryptjs = require("bcryptjs");
|
|
29
|
+
var import_jsonwebtoken = require("jsonwebtoken");
|
|
30
|
+
var import_server = require("next/server");
|
|
31
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
32
|
+
const body = { accessToken, refreshToken };
|
|
33
|
+
const response = import_server.NextResponse.json(body, {
|
|
34
|
+
status: 200
|
|
35
|
+
});
|
|
36
|
+
if (!accessToken) {
|
|
37
|
+
response.cookies.set("refresh", "", {
|
|
38
|
+
secure: false,
|
|
39
|
+
httpOnly: true,
|
|
40
|
+
expires: 0
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (refreshToken !== void 0) {
|
|
44
|
+
response.cookies.set("refresh", refreshToken, {
|
|
45
|
+
secure: false,
|
|
46
|
+
httpOnly: true,
|
|
47
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return response;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/auth/email/routes/delete.ts
|
|
54
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
55
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
56
|
+
0 && (module.exports = {
|
|
57
|
+
getDeleteRoute
|
|
58
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/auth/email/token.ts
|
|
2
|
+
import { compare } from "bcryptjs";
|
|
3
|
+
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
4
|
+
import { NextResponse } from "next/server";
|
|
5
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
6
|
+
const body = { accessToken, refreshToken };
|
|
7
|
+
const response = NextResponse.json(body, {
|
|
8
|
+
status: 200
|
|
9
|
+
});
|
|
10
|
+
if (!accessToken) {
|
|
11
|
+
response.cookies.set("refresh", "", {
|
|
12
|
+
secure: false,
|
|
13
|
+
httpOnly: true,
|
|
14
|
+
expires: 0
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
if (refreshToken !== void 0) {
|
|
18
|
+
response.cookies.set("refresh", refreshToken, {
|
|
19
|
+
secure: false,
|
|
20
|
+
httpOnly: true,
|
|
21
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return response;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/auth/email/routes/delete.ts
|
|
28
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
29
|
+
export {
|
|
30
|
+
getDeleteRoute
|
|
31
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/auth/email/routes/get.ts
|
|
21
|
+
var get_exports = {};
|
|
22
|
+
__export(get_exports, {
|
|
23
|
+
getGetRoute: () => getGetRoute
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(get_exports);
|
|
26
|
+
|
|
27
|
+
// src/auth/email/token.ts
|
|
28
|
+
var import_bcryptjs = require("bcryptjs");
|
|
29
|
+
var import_jsonwebtoken = require("jsonwebtoken");
|
|
30
|
+
var import_server = require("next/server");
|
|
31
|
+
function generateAccessToken(id, signingKey) {
|
|
32
|
+
return (0, import_jsonwebtoken.sign)({ id }, signingKey, {
|
|
33
|
+
expiresIn: "2h"
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
37
|
+
const body = { accessToken, refreshToken };
|
|
38
|
+
const response = import_server.NextResponse.json(body, {
|
|
39
|
+
status: 200
|
|
40
|
+
});
|
|
41
|
+
if (!accessToken) {
|
|
42
|
+
response.cookies.set("refresh", "", {
|
|
43
|
+
secure: false,
|
|
44
|
+
httpOnly: true,
|
|
45
|
+
expires: 0
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
if (refreshToken !== void 0) {
|
|
49
|
+
response.cookies.set("refresh", refreshToken, {
|
|
50
|
+
secure: false,
|
|
51
|
+
httpOnly: true,
|
|
52
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
58
|
+
if (refreshToken)
|
|
59
|
+
try {
|
|
60
|
+
const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
|
|
61
|
+
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
62
|
+
return decoded.id;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/auth/email/routes/get.ts
|
|
71
|
+
var getGetRoute = (options) => (req) => {
|
|
72
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
73
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
74
|
+
if (userID)
|
|
75
|
+
return getTokenizedResponse(
|
|
76
|
+
generateAccessToken(userID, options.signingKey)
|
|
77
|
+
);
|
|
78
|
+
return getTokenizedResponse();
|
|
79
|
+
};
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
getGetRoute
|
|
83
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as next_server from 'next/server';
|
|
2
|
+
import { NextRequest } from 'next/server';
|
|
3
|
+
import { InitRoutesOptions } from '../types.mjs';
|
|
4
|
+
import '../../types.mjs';
|
|
5
|
+
|
|
6
|
+
declare const getGetRoute: (options: InitRoutesOptions) => (req: NextRequest) => next_server.NextResponse<{
|
|
7
|
+
accessToken: string | undefined;
|
|
8
|
+
refreshToken: string | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
|
|
11
|
+
export { getGetRoute };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as next_server from 'next/server';
|
|
2
|
+
import { NextRequest } from 'next/server';
|
|
3
|
+
import { InitRoutesOptions } from '../types.js';
|
|
4
|
+
import '../../types.js';
|
|
5
|
+
|
|
6
|
+
declare const getGetRoute: (options: InitRoutesOptions) => (req: NextRequest) => next_server.NextResponse<{
|
|
7
|
+
accessToken: string | undefined;
|
|
8
|
+
refreshToken: string | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
|
|
11
|
+
export { getGetRoute };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/auth/email/token.ts
|
|
2
|
+
import { compare } from "bcryptjs";
|
|
3
|
+
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
4
|
+
import { NextResponse } from "next/server";
|
|
5
|
+
function generateAccessToken(id, signingKey) {
|
|
6
|
+
return sign({ id }, signingKey, {
|
|
7
|
+
expiresIn: "2h"
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
11
|
+
const body = { accessToken, refreshToken };
|
|
12
|
+
const response = NextResponse.json(body, {
|
|
13
|
+
status: 200
|
|
14
|
+
});
|
|
15
|
+
if (!accessToken) {
|
|
16
|
+
response.cookies.set("refresh", "", {
|
|
17
|
+
secure: false,
|
|
18
|
+
httpOnly: true,
|
|
19
|
+
expires: 0
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (refreshToken !== void 0) {
|
|
23
|
+
response.cookies.set("refresh", refreshToken, {
|
|
24
|
+
secure: false,
|
|
25
|
+
httpOnly: true,
|
|
26
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return response;
|
|
30
|
+
}
|
|
31
|
+
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
32
|
+
if (refreshToken)
|
|
33
|
+
try {
|
|
34
|
+
const decoded = verify(refreshToken, refreshKey);
|
|
35
|
+
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
36
|
+
return decoded.id;
|
|
37
|
+
} catch (e) {
|
|
38
|
+
if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/auth/email/routes/get.ts
|
|
45
|
+
var getGetRoute = (options) => (req) => {
|
|
46
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
47
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
48
|
+
if (userID)
|
|
49
|
+
return getTokenizedResponse(
|
|
50
|
+
generateAccessToken(userID, options.signingKey)
|
|
51
|
+
);
|
|
52
|
+
return getTokenizedResponse();
|
|
53
|
+
};
|
|
54
|
+
export {
|
|
55
|
+
getGetRoute
|
|
56
|
+
};
|