naystack 1.1.11 → 1.1.12-beta.1
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.d.mts +7 -23
- package/dist/graphql/index.d.ts +7 -23
- package/dist/graphql/init.cjs.js +124 -0
- package/dist/graphql/init.d.mts +16 -0
- package/dist/graphql/init.d.ts +16 -0
- package/dist/graphql/init.esm.js +104 -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,149 @@
|
|
|
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/post.ts
|
|
21
|
+
var post_exports = {};
|
|
22
|
+
__export(post_exports, {
|
|
23
|
+
getPostRoute: () => getPostRoute
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(post_exports);
|
|
26
|
+
var import_bcryptjs2 = require("bcryptjs");
|
|
27
|
+
|
|
28
|
+
// src/auth/utils/errors.ts
|
|
29
|
+
var import_server = require("next/server");
|
|
30
|
+
function handleError(status, message, onError) {
|
|
31
|
+
const res = onError?.({ status, message });
|
|
32
|
+
if (res) return res;
|
|
33
|
+
return new import_server.NextResponse(message, { status });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/auth/email/token.ts
|
|
37
|
+
var import_bcryptjs = require("bcryptjs");
|
|
38
|
+
var import_jsonwebtoken = require("jsonwebtoken");
|
|
39
|
+
var import_server2 = require("next/server");
|
|
40
|
+
function generateAccessToken(id, signingKey) {
|
|
41
|
+
return (0, import_jsonwebtoken.sign)({ id }, signingKey, {
|
|
42
|
+
expiresIn: "2h"
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function generateRefreshToken(id, refreshKey) {
|
|
46
|
+
return (0, import_jsonwebtoken.sign)({ id }, refreshKey);
|
|
47
|
+
}
|
|
48
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
49
|
+
const body = { accessToken, refreshToken };
|
|
50
|
+
const response = import_server2.NextResponse.json(body, {
|
|
51
|
+
status: 200
|
|
52
|
+
});
|
|
53
|
+
if (!accessToken) {
|
|
54
|
+
response.cookies.set("refresh", "", {
|
|
55
|
+
secure: false,
|
|
56
|
+
httpOnly: true,
|
|
57
|
+
expires: 0
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (refreshToken !== void 0) {
|
|
61
|
+
response.cookies.set("refresh", refreshToken, {
|
|
62
|
+
secure: false,
|
|
63
|
+
httpOnly: true,
|
|
64
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return response;
|
|
68
|
+
}
|
|
69
|
+
function verifyUser(user, password) {
|
|
70
|
+
if (!user.password) return false;
|
|
71
|
+
return (0, import_bcryptjs.compare)(password, user.password);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/auth/email/utils.ts
|
|
75
|
+
async function massageRequest(req, options) {
|
|
76
|
+
const data = await req.json();
|
|
77
|
+
if (!data.email || !data.password)
|
|
78
|
+
return {
|
|
79
|
+
error: handleError(400, "Missing email or password", options.onError)
|
|
80
|
+
};
|
|
81
|
+
if (options.turnstileKey) {
|
|
82
|
+
if (!data.captchaToken)
|
|
83
|
+
return { error: handleError(400, "Missing captcha", options.onError) };
|
|
84
|
+
if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
|
|
85
|
+
return {
|
|
86
|
+
error: handleError(400, "Invalid captcha", options.onError)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
data: {
|
|
91
|
+
email: data.email,
|
|
92
|
+
password: data.password,
|
|
93
|
+
...data
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
async function verifyCaptcha(token, secret) {
|
|
98
|
+
const res = await fetch(
|
|
99
|
+
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
100
|
+
{
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: {
|
|
103
|
+
"Content-Type": "application/json"
|
|
104
|
+
},
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
secret,
|
|
107
|
+
response: token
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
if (res.ok) {
|
|
112
|
+
const data = await res.json();
|
|
113
|
+
return data.success;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/auth/email/routes/post.ts
|
|
119
|
+
var getPostRoute = (options) => async (req) => {
|
|
120
|
+
const { data, error } = await massageRequest(req, options);
|
|
121
|
+
if (error || !data) return error;
|
|
122
|
+
const existingUser = await options.getUser(data.email);
|
|
123
|
+
if (existingUser) {
|
|
124
|
+
if (await verifyUser(existingUser, data.password)) {
|
|
125
|
+
return getTokenizedResponse(
|
|
126
|
+
generateAccessToken(existingUser.id, options.signingKey),
|
|
127
|
+
generateRefreshToken(existingUser.id, options.refreshKey)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return handleError(400, "A user already exists", options.onError);
|
|
131
|
+
}
|
|
132
|
+
const encryptedPassword = await (0, import_bcryptjs2.hash)(data.password, 10);
|
|
133
|
+
const newUser = await options.createUser({
|
|
134
|
+
...data,
|
|
135
|
+
password: encryptedPassword
|
|
136
|
+
});
|
|
137
|
+
if (newUser) {
|
|
138
|
+
options.onSignUp?.(newUser);
|
|
139
|
+
return getTokenizedResponse(
|
|
140
|
+
generateAccessToken(newUser.id, options.signingKey),
|
|
141
|
+
generateRefreshToken(newUser.id, options.refreshKey)
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return getTokenizedResponse();
|
|
145
|
+
};
|
|
146
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
147
|
+
0 && (module.exports = {
|
|
148
|
+
getPostRoute
|
|
149
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
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 getPostRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
|
+
|
|
8
|
+
export { getPostRoute };
|
|
@@ -0,0 +1,8 @@
|
|
|
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 getPostRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
|
+
|
|
8
|
+
export { getPostRoute };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// src/auth/email/routes/post.ts
|
|
2
|
+
import { hash } from "bcryptjs";
|
|
3
|
+
|
|
4
|
+
// src/auth/utils/errors.ts
|
|
5
|
+
import { NextResponse } from "next/server";
|
|
6
|
+
function handleError(status, message, onError) {
|
|
7
|
+
const res = onError?.({ status, message });
|
|
8
|
+
if (res) return res;
|
|
9
|
+
return new NextResponse(message, { status });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/auth/email/token.ts
|
|
13
|
+
import { compare } from "bcryptjs";
|
|
14
|
+
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
15
|
+
import { NextResponse as NextResponse2 } from "next/server";
|
|
16
|
+
function generateAccessToken(id, signingKey) {
|
|
17
|
+
return sign({ id }, signingKey, {
|
|
18
|
+
expiresIn: "2h"
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function generateRefreshToken(id, refreshKey) {
|
|
22
|
+
return sign({ id }, refreshKey);
|
|
23
|
+
}
|
|
24
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
25
|
+
const body = { accessToken, refreshToken };
|
|
26
|
+
const response = NextResponse2.json(body, {
|
|
27
|
+
status: 200
|
|
28
|
+
});
|
|
29
|
+
if (!accessToken) {
|
|
30
|
+
response.cookies.set("refresh", "", {
|
|
31
|
+
secure: false,
|
|
32
|
+
httpOnly: true,
|
|
33
|
+
expires: 0
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (refreshToken !== void 0) {
|
|
37
|
+
response.cookies.set("refresh", refreshToken, {
|
|
38
|
+
secure: false,
|
|
39
|
+
httpOnly: true,
|
|
40
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return response;
|
|
44
|
+
}
|
|
45
|
+
function verifyUser(user, password) {
|
|
46
|
+
if (!user.password) return false;
|
|
47
|
+
return compare(password, user.password);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/auth/email/utils.ts
|
|
51
|
+
async function massageRequest(req, options) {
|
|
52
|
+
const data = await req.json();
|
|
53
|
+
if (!data.email || !data.password)
|
|
54
|
+
return {
|
|
55
|
+
error: handleError(400, "Missing email or password", options.onError)
|
|
56
|
+
};
|
|
57
|
+
if (options.turnstileKey) {
|
|
58
|
+
if (!data.captchaToken)
|
|
59
|
+
return { error: handleError(400, "Missing captcha", options.onError) };
|
|
60
|
+
if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
|
|
61
|
+
return {
|
|
62
|
+
error: handleError(400, "Invalid captcha", options.onError)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
data: {
|
|
67
|
+
email: data.email,
|
|
68
|
+
password: data.password,
|
|
69
|
+
...data
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async function verifyCaptcha(token, secret) {
|
|
74
|
+
const res = await fetch(
|
|
75
|
+
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
76
|
+
{
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: {
|
|
79
|
+
"Content-Type": "application/json"
|
|
80
|
+
},
|
|
81
|
+
body: JSON.stringify({
|
|
82
|
+
secret,
|
|
83
|
+
response: token
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
if (res.ok) {
|
|
88
|
+
const data = await res.json();
|
|
89
|
+
return data.success;
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/auth/email/routes/post.ts
|
|
95
|
+
var getPostRoute = (options) => async (req) => {
|
|
96
|
+
const { data, error } = await massageRequest(req, options);
|
|
97
|
+
if (error || !data) return error;
|
|
98
|
+
const existingUser = await options.getUser(data.email);
|
|
99
|
+
if (existingUser) {
|
|
100
|
+
if (await verifyUser(existingUser, data.password)) {
|
|
101
|
+
return getTokenizedResponse(
|
|
102
|
+
generateAccessToken(existingUser.id, options.signingKey),
|
|
103
|
+
generateRefreshToken(existingUser.id, options.refreshKey)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return handleError(400, "A user already exists", options.onError);
|
|
107
|
+
}
|
|
108
|
+
const encryptedPassword = await hash(data.password, 10);
|
|
109
|
+
const newUser = await options.createUser({
|
|
110
|
+
...data,
|
|
111
|
+
password: encryptedPassword
|
|
112
|
+
});
|
|
113
|
+
if (newUser) {
|
|
114
|
+
options.onSignUp?.(newUser);
|
|
115
|
+
return getTokenizedResponse(
|
|
116
|
+
generateAccessToken(newUser.id, options.signingKey),
|
|
117
|
+
generateRefreshToken(newUser.id, options.refreshKey)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return getTokenizedResponse();
|
|
121
|
+
};
|
|
122
|
+
export {
|
|
123
|
+
getPostRoute
|
|
124
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
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/put.ts
|
|
21
|
+
var put_exports = {};
|
|
22
|
+
__export(put_exports, {
|
|
23
|
+
getPutRoute: () => getPutRoute
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(put_exports);
|
|
26
|
+
|
|
27
|
+
// src/auth/utils/errors.ts
|
|
28
|
+
var import_server = require("next/server");
|
|
29
|
+
function handleError(status, message, onError) {
|
|
30
|
+
const res = onError?.({ status, message });
|
|
31
|
+
if (res) return res;
|
|
32
|
+
return new import_server.NextResponse(message, { status });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/auth/email/token.ts
|
|
36
|
+
var import_bcryptjs = require("bcryptjs");
|
|
37
|
+
var import_jsonwebtoken = require("jsonwebtoken");
|
|
38
|
+
var import_server2 = require("next/server");
|
|
39
|
+
function generateAccessToken(id, signingKey) {
|
|
40
|
+
return (0, import_jsonwebtoken.sign)({ id }, signingKey, {
|
|
41
|
+
expiresIn: "2h"
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function generateRefreshToken(id, refreshKey) {
|
|
45
|
+
return (0, import_jsonwebtoken.sign)({ id }, refreshKey);
|
|
46
|
+
}
|
|
47
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
48
|
+
const body = { accessToken, refreshToken };
|
|
49
|
+
const response = import_server2.NextResponse.json(body, {
|
|
50
|
+
status: 200
|
|
51
|
+
});
|
|
52
|
+
if (!accessToken) {
|
|
53
|
+
response.cookies.set("refresh", "", {
|
|
54
|
+
secure: false,
|
|
55
|
+
httpOnly: true,
|
|
56
|
+
expires: 0
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (refreshToken !== void 0) {
|
|
60
|
+
response.cookies.set("refresh", refreshToken, {
|
|
61
|
+
secure: false,
|
|
62
|
+
httpOnly: true,
|
|
63
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return response;
|
|
67
|
+
}
|
|
68
|
+
function verifyUser(user, password) {
|
|
69
|
+
if (!user.password) return false;
|
|
70
|
+
return (0, import_bcryptjs.compare)(password, user.password);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/auth/email/utils.ts
|
|
74
|
+
async function massageRequest(req, options) {
|
|
75
|
+
const data = await req.json();
|
|
76
|
+
if (!data.email || !data.password)
|
|
77
|
+
return {
|
|
78
|
+
error: handleError(400, "Missing email or password", options.onError)
|
|
79
|
+
};
|
|
80
|
+
if (options.turnstileKey) {
|
|
81
|
+
if (!data.captchaToken)
|
|
82
|
+
return { error: handleError(400, "Missing captcha", options.onError) };
|
|
83
|
+
if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
|
|
84
|
+
return {
|
|
85
|
+
error: handleError(400, "Invalid captcha", options.onError)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
data: {
|
|
90
|
+
email: data.email,
|
|
91
|
+
password: data.password,
|
|
92
|
+
...data
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async function verifyCaptcha(token, secret) {
|
|
97
|
+
const res = await fetch(
|
|
98
|
+
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
99
|
+
{
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: {
|
|
102
|
+
"Content-Type": "application/json"
|
|
103
|
+
},
|
|
104
|
+
body: JSON.stringify({
|
|
105
|
+
secret,
|
|
106
|
+
response: token
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
if (res.ok) {
|
|
111
|
+
const data = await res.json();
|
|
112
|
+
return data.success;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/auth/email/routes/put.ts
|
|
118
|
+
var getPutRoute = (options) => async (req) => {
|
|
119
|
+
const { data, error } = await massageRequest(req, options);
|
|
120
|
+
if (error || !data) return error;
|
|
121
|
+
const user = await options.getUser(data.email);
|
|
122
|
+
if (!user)
|
|
123
|
+
return handleError(400, "A user does not exist", options.onError);
|
|
124
|
+
if (await verifyUser(user, data.password)) {
|
|
125
|
+
return getTokenizedResponse(
|
|
126
|
+
generateAccessToken(user.id, options.signingKey),
|
|
127
|
+
generateRefreshToken(user.id, options.refreshKey)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return handleError(403, "Invalid password", options.onError);
|
|
131
|
+
};
|
|
132
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
133
|
+
0 && (module.exports = {
|
|
134
|
+
getPutRoute
|
|
135
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
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 getPutRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
|
+
|
|
8
|
+
export { getPutRoute };
|
|
@@ -0,0 +1,8 @@
|
|
|
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 getPutRoute: (options: InitRoutesOptions) => (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
7
|
+
|
|
8
|
+
export { getPutRoute };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// src/auth/utils/errors.ts
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
function handleError(status, message, onError) {
|
|
4
|
+
const res = onError?.({ status, message });
|
|
5
|
+
if (res) return res;
|
|
6
|
+
return new NextResponse(message, { status });
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/auth/email/token.ts
|
|
10
|
+
import { compare } from "bcryptjs";
|
|
11
|
+
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
12
|
+
import { NextResponse as NextResponse2 } from "next/server";
|
|
13
|
+
function generateAccessToken(id, signingKey) {
|
|
14
|
+
return sign({ id }, signingKey, {
|
|
15
|
+
expiresIn: "2h"
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function generateRefreshToken(id, refreshKey) {
|
|
19
|
+
return sign({ id }, refreshKey);
|
|
20
|
+
}
|
|
21
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
22
|
+
const body = { accessToken, refreshToken };
|
|
23
|
+
const response = NextResponse2.json(body, {
|
|
24
|
+
status: 200
|
|
25
|
+
});
|
|
26
|
+
if (!accessToken) {
|
|
27
|
+
response.cookies.set("refresh", "", {
|
|
28
|
+
secure: false,
|
|
29
|
+
httpOnly: true,
|
|
30
|
+
expires: 0
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (refreshToken !== void 0) {
|
|
34
|
+
response.cookies.set("refresh", refreshToken, {
|
|
35
|
+
secure: false,
|
|
36
|
+
httpOnly: true,
|
|
37
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return response;
|
|
41
|
+
}
|
|
42
|
+
function verifyUser(user, password) {
|
|
43
|
+
if (!user.password) return false;
|
|
44
|
+
return compare(password, user.password);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/auth/email/utils.ts
|
|
48
|
+
async function massageRequest(req, options) {
|
|
49
|
+
const data = await req.json();
|
|
50
|
+
if (!data.email || !data.password)
|
|
51
|
+
return {
|
|
52
|
+
error: handleError(400, "Missing email or password", options.onError)
|
|
53
|
+
};
|
|
54
|
+
if (options.turnstileKey) {
|
|
55
|
+
if (!data.captchaToken)
|
|
56
|
+
return { error: handleError(400, "Missing captcha", options.onError) };
|
|
57
|
+
if (!await verifyCaptcha(data.captchaToken, options.turnstileKey))
|
|
58
|
+
return {
|
|
59
|
+
error: handleError(400, "Invalid captcha", options.onError)
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
data: {
|
|
64
|
+
email: data.email,
|
|
65
|
+
password: data.password,
|
|
66
|
+
...data
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
async function verifyCaptcha(token, secret) {
|
|
71
|
+
const res = await fetch(
|
|
72
|
+
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
73
|
+
{
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: {
|
|
76
|
+
"Content-Type": "application/json"
|
|
77
|
+
},
|
|
78
|
+
body: JSON.stringify({
|
|
79
|
+
secret,
|
|
80
|
+
response: token
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
if (res.ok) {
|
|
85
|
+
const data = await res.json();
|
|
86
|
+
return data.success;
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/auth/email/routes/put.ts
|
|
92
|
+
var getPutRoute = (options) => async (req) => {
|
|
93
|
+
const { data, error } = await massageRequest(req, options);
|
|
94
|
+
if (error || !data) return error;
|
|
95
|
+
const user = await options.getUser(data.email);
|
|
96
|
+
if (!user)
|
|
97
|
+
return handleError(400, "A user does not exist", options.onError);
|
|
98
|
+
if (await verifyUser(user, data.password)) {
|
|
99
|
+
return getTokenizedResponse(
|
|
100
|
+
generateAccessToken(user.id, options.signingKey),
|
|
101
|
+
generateRefreshToken(user.id, options.refreshKey)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
return handleError(403, "Invalid password", options.onError);
|
|
105
|
+
};
|
|
106
|
+
export {
|
|
107
|
+
getPutRoute
|
|
108
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
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/token.ts
|
|
21
|
+
var token_exports = {};
|
|
22
|
+
__export(token_exports, {
|
|
23
|
+
generateAccessToken: () => generateAccessToken,
|
|
24
|
+
generateRefreshToken: () => generateRefreshToken,
|
|
25
|
+
getTokenizedResponse: () => getTokenizedResponse,
|
|
26
|
+
getUserIdFromRefreshToken: () => getUserIdFromRefreshToken,
|
|
27
|
+
verifyUser: () => verifyUser
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(token_exports);
|
|
30
|
+
var import_bcryptjs = require("bcryptjs");
|
|
31
|
+
var import_jsonwebtoken = require("jsonwebtoken");
|
|
32
|
+
var import_server = require("next/server");
|
|
33
|
+
function generateAccessToken(id, signingKey) {
|
|
34
|
+
return (0, import_jsonwebtoken.sign)({ id }, signingKey, {
|
|
35
|
+
expiresIn: "2h"
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function generateRefreshToken(id, refreshKey) {
|
|
39
|
+
return (0, import_jsonwebtoken.sign)({ id }, refreshKey);
|
|
40
|
+
}
|
|
41
|
+
function getTokenizedResponse(accessToken, refreshToken) {
|
|
42
|
+
const body = { accessToken, refreshToken };
|
|
43
|
+
const response = import_server.NextResponse.json(body, {
|
|
44
|
+
status: 200
|
|
45
|
+
});
|
|
46
|
+
if (!accessToken) {
|
|
47
|
+
response.cookies.set("refresh", "", {
|
|
48
|
+
secure: false,
|
|
49
|
+
httpOnly: true,
|
|
50
|
+
expires: 0
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (refreshToken !== void 0) {
|
|
54
|
+
response.cookies.set("refresh", refreshToken, {
|
|
55
|
+
secure: false,
|
|
56
|
+
httpOnly: true,
|
|
57
|
+
expires: refreshToken === "" ? 0 : new Date(Date.now() + 60 * 60 * 24 * 365 * 1e3)
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
63
|
+
if (refreshToken)
|
|
64
|
+
try {
|
|
65
|
+
const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
|
|
66
|
+
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
67
|
+
return decoded.id;
|
|
68
|
+
} catch (e) {
|
|
69
|
+
if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function verifyUser(user, password) {
|
|
75
|
+
if (!user.password) return false;
|
|
76
|
+
return (0, import_bcryptjs.compare)(password, user.password);
|
|
77
|
+
}
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
generateAccessToken,
|
|
81
|
+
generateRefreshToken,
|
|
82
|
+
getTokenizedResponse,
|
|
83
|
+
getUserIdFromRefreshToken,
|
|
84
|
+
verifyUser
|
|
85
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { UserOutput } from '../types.mjs';
|
|
3
|
+
|
|
4
|
+
declare function generateAccessToken(id: number, signingKey: string): string;
|
|
5
|
+
declare function generateRefreshToken(id: number, refreshKey: string): string;
|
|
6
|
+
declare function getTokenizedResponse(accessToken?: string, refreshToken?: string): NextResponse<{
|
|
7
|
+
accessToken: string | undefined;
|
|
8
|
+
refreshToken: string | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
declare function getUserIdFromRefreshToken(refreshKey: string, refreshToken?: string): number | null;
|
|
11
|
+
declare function verifyUser(user: UserOutput, password: string): false | Promise<boolean>;
|
|
12
|
+
|
|
13
|
+
export { generateAccessToken, generateRefreshToken, getTokenizedResponse, getUserIdFromRefreshToken, verifyUser };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NextResponse } from 'next/server';
|
|
2
|
+
import { UserOutput } from '../types.js';
|
|
3
|
+
|
|
4
|
+
declare function generateAccessToken(id: number, signingKey: string): string;
|
|
5
|
+
declare function generateRefreshToken(id: number, refreshKey: string): string;
|
|
6
|
+
declare function getTokenizedResponse(accessToken?: string, refreshToken?: string): NextResponse<{
|
|
7
|
+
accessToken: string | undefined;
|
|
8
|
+
refreshToken: string | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
declare function getUserIdFromRefreshToken(refreshKey: string, refreshToken?: string): number | null;
|
|
11
|
+
declare function verifyUser(user: UserOutput, password: string): false | Promise<boolean>;
|
|
12
|
+
|
|
13
|
+
export { generateAccessToken, generateRefreshToken, getTokenizedResponse, getUserIdFromRefreshToken, verifyUser };
|