naystack 1.1.12-beta.2 → 1.1.12-beta.4
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 +41 -18
- package/dist/auth/email/index.d.mts +8 -3
- package/dist/auth/email/index.d.ts +8 -3
- package/dist/auth/email/index.esm.js +41 -18
- package/dist/auth/email/routes/post.cjs.js +1 -0
- package/dist/auth/email/routes/post.esm.js +1 -0
- package/dist/auth/email/routes/put.cjs.js +1 -0
- package/dist/auth/email/routes/put.esm.js +1 -0
- package/dist/auth/email/utils.cjs.js +43 -2
- package/dist/auth/email/utils.d.mts +5 -1
- package/dist/auth/email/utils.d.ts +5 -1
- package/dist/auth/email/utils.esm.js +44 -2
- package/dist/auth/index.cjs.js +41 -18
- package/dist/auth/index.esm.js +41 -18
- package/dist/graphql/index.cjs.js +4 -55
- package/dist/graphql/index.d.mts +0 -1
- package/dist/graphql/index.d.ts +0 -1
- package/dist/graphql/index.esm.js +2 -53
- package/dist/graphql/init.cjs.js +4 -55
- package/dist/graphql/init.d.mts +3 -3
- package/dist/graphql/init.d.ts +3 -3
- package/dist/graphql/init.esm.js +2 -53
- package/package.json +2 -2
- package/dist/graphql/context.cjs.js +0 -76
- package/dist/graphql/context.d.mts +0 -6
- package/dist/graphql/context.d.ts +0 -6
- package/dist/graphql/context.esm.js +0 -51
|
@@ -24,6 +24,9 @@ __export(email_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(email_exports);
|
|
26
26
|
|
|
27
|
+
// src/auth/email/utils.ts
|
|
28
|
+
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
29
|
+
|
|
27
30
|
// src/auth/email/token.ts
|
|
28
31
|
var import_bcryptjs = require("bcryptjs");
|
|
29
32
|
var import_jsonwebtoken = require("jsonwebtoken");
|
|
@@ -74,23 +77,6 @@ function verifyUser(user, password) {
|
|
|
74
77
|
return (0, import_bcryptjs.compare)(password, user.password);
|
|
75
78
|
}
|
|
76
79
|
|
|
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
80
|
// src/auth/utils/errors.ts
|
|
95
81
|
var import_server2 = require("next/server");
|
|
96
82
|
function handleError(status, message, onError) {
|
|
@@ -142,8 +128,44 @@ async function verifyCaptcha(token, secret) {
|
|
|
142
128
|
}
|
|
143
129
|
return false;
|
|
144
130
|
}
|
|
131
|
+
var getUserContext = (refreshKey, signingKey, req) => {
|
|
132
|
+
const bearer = req.headers.get("authorization");
|
|
133
|
+
if (!bearer) {
|
|
134
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
135
|
+
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
136
|
+
if (userId) return { refreshUserID: userId };
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const token = bearer.slice(7);
|
|
140
|
+
try {
|
|
141
|
+
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
142
|
+
if (typeof res === "string") {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
accessUserId: res.id
|
|
147
|
+
};
|
|
148
|
+
} catch {
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// src/auth/email/routes/delete.ts
|
|
154
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
155
|
+
|
|
156
|
+
// src/auth/email/routes/get.ts
|
|
157
|
+
var getGetRoute = (options) => (req) => {
|
|
158
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
159
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
160
|
+
if (userID)
|
|
161
|
+
return getTokenizedResponse(
|
|
162
|
+
generateAccessToken(userID, options.signingKey)
|
|
163
|
+
);
|
|
164
|
+
return getTokenizedResponse();
|
|
165
|
+
};
|
|
145
166
|
|
|
146
167
|
// src/auth/email/routes/post.ts
|
|
168
|
+
var import_bcryptjs2 = require("bcryptjs");
|
|
147
169
|
var getPostRoute = (options) => async (req) => {
|
|
148
170
|
const { data, error } = await massageRequest(req, options);
|
|
149
171
|
if (error || !data) return error;
|
|
@@ -194,7 +216,8 @@ function getEmailAuthRoutes(options) {
|
|
|
194
216
|
GET: getGetRoute(options),
|
|
195
217
|
POST: getPostRoute(options),
|
|
196
218
|
PUT: getPutRoute(options),
|
|
197
|
-
DELETE: getDeleteRoute()
|
|
219
|
+
DELETE: getDeleteRoute(),
|
|
220
|
+
getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
|
|
198
221
|
};
|
|
199
222
|
}
|
|
200
223
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import * as next_server from 'next/server';
|
|
2
|
+
import { NextRequest } from 'next/server';
|
|
2
3
|
import { InitRoutesOptions } from './types.mjs';
|
|
3
4
|
import '../types.mjs';
|
|
4
5
|
|
|
5
6
|
declare function getEmailAuthRoutes(options: InitRoutesOptions): {
|
|
6
|
-
GET: (req:
|
|
7
|
+
GET: (req: NextRequest) => next_server.NextResponse<{
|
|
7
8
|
accessToken: string | undefined;
|
|
8
9
|
refreshToken: string | undefined;
|
|
9
10
|
}>;
|
|
10
|
-
POST: (req:
|
|
11
|
-
PUT: (req:
|
|
11
|
+
POST: (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
12
|
+
PUT: (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
12
13
|
DELETE: () => next_server.NextResponse<{
|
|
13
14
|
accessToken: string | undefined;
|
|
14
15
|
refreshToken: string | undefined;
|
|
15
16
|
}>;
|
|
17
|
+
getUserIdFromRequest: (req: NextRequest) => {
|
|
18
|
+
refreshUserID?: number;
|
|
19
|
+
accessUserId?: number;
|
|
20
|
+
} | null;
|
|
16
21
|
};
|
|
17
22
|
|
|
18
23
|
export { getEmailAuthRoutes };
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import * as next_server from 'next/server';
|
|
2
|
+
import { NextRequest } from 'next/server';
|
|
2
3
|
import { InitRoutesOptions } from './types.js';
|
|
3
4
|
import '../types.js';
|
|
4
5
|
|
|
5
6
|
declare function getEmailAuthRoutes(options: InitRoutesOptions): {
|
|
6
|
-
GET: (req:
|
|
7
|
+
GET: (req: NextRequest) => next_server.NextResponse<{
|
|
7
8
|
accessToken: string | undefined;
|
|
8
9
|
refreshToken: string | undefined;
|
|
9
10
|
}>;
|
|
10
|
-
POST: (req:
|
|
11
|
-
PUT: (req:
|
|
11
|
+
POST: (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
12
|
+
PUT: (req: NextRequest) => Promise<next_server.NextResponse<unknown> | undefined>;
|
|
12
13
|
DELETE: () => next_server.NextResponse<{
|
|
13
14
|
accessToken: string | undefined;
|
|
14
15
|
refreshToken: string | undefined;
|
|
15
16
|
}>;
|
|
17
|
+
getUserIdFromRequest: (req: NextRequest) => {
|
|
18
|
+
refreshUserID?: number;
|
|
19
|
+
accessUserId?: number;
|
|
20
|
+
} | null;
|
|
16
21
|
};
|
|
17
22
|
|
|
18
23
|
export { getEmailAuthRoutes };
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// src/auth/email/utils.ts
|
|
2
|
+
import { verify as verify2 } from "jsonwebtoken";
|
|
3
|
+
|
|
1
4
|
// src/auth/email/token.ts
|
|
2
5
|
import { compare } from "bcryptjs";
|
|
3
6
|
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
@@ -48,23 +51,6 @@ function verifyUser(user, password) {
|
|
|
48
51
|
return compare(password, user.password);
|
|
49
52
|
}
|
|
50
53
|
|
|
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
54
|
// src/auth/utils/errors.ts
|
|
69
55
|
import { NextResponse as NextResponse2 } from "next/server";
|
|
70
56
|
function handleError(status, message, onError) {
|
|
@@ -116,8 +102,44 @@ async function verifyCaptcha(token, secret) {
|
|
|
116
102
|
}
|
|
117
103
|
return false;
|
|
118
104
|
}
|
|
105
|
+
var getUserContext = (refreshKey, signingKey, req) => {
|
|
106
|
+
const bearer = req.headers.get("authorization");
|
|
107
|
+
if (!bearer) {
|
|
108
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
109
|
+
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
110
|
+
if (userId) return { refreshUserID: userId };
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
const token = bearer.slice(7);
|
|
114
|
+
try {
|
|
115
|
+
const res = verify2(token, signingKey);
|
|
116
|
+
if (typeof res === "string") {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
accessUserId: res.id
|
|
121
|
+
};
|
|
122
|
+
} catch {
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/auth/email/routes/delete.ts
|
|
128
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
129
|
+
|
|
130
|
+
// src/auth/email/routes/get.ts
|
|
131
|
+
var getGetRoute = (options) => (req) => {
|
|
132
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
133
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
134
|
+
if (userID)
|
|
135
|
+
return getTokenizedResponse(
|
|
136
|
+
generateAccessToken(userID, options.signingKey)
|
|
137
|
+
);
|
|
138
|
+
return getTokenizedResponse();
|
|
139
|
+
};
|
|
119
140
|
|
|
120
141
|
// src/auth/email/routes/post.ts
|
|
142
|
+
import { hash } from "bcryptjs";
|
|
121
143
|
var getPostRoute = (options) => async (req) => {
|
|
122
144
|
const { data, error } = await massageRequest(req, options);
|
|
123
145
|
if (error || !data) return error;
|
|
@@ -168,7 +190,8 @@ function getEmailAuthRoutes(options) {
|
|
|
168
190
|
GET: getGetRoute(options),
|
|
169
191
|
POST: getPostRoute(options),
|
|
170
192
|
PUT: getPutRoute(options),
|
|
171
|
-
DELETE: getDeleteRoute()
|
|
193
|
+
DELETE: getDeleteRoute(),
|
|
194
|
+
getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
|
|
172
195
|
};
|
|
173
196
|
}
|
|
174
197
|
export {
|
|
@@ -20,17 +20,36 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/auth/email/utils.ts
|
|
21
21
|
var utils_exports = {};
|
|
22
22
|
__export(utils_exports, {
|
|
23
|
+
getUserContext: () => getUserContext,
|
|
23
24
|
massageRequest: () => massageRequest,
|
|
24
25
|
verifyCaptcha: () => verifyCaptcha
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(utils_exports);
|
|
28
|
+
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
27
29
|
|
|
28
|
-
// src/auth/
|
|
30
|
+
// src/auth/email/token.ts
|
|
31
|
+
var import_bcryptjs = require("bcryptjs");
|
|
32
|
+
var import_jsonwebtoken = require("jsonwebtoken");
|
|
29
33
|
var import_server = require("next/server");
|
|
34
|
+
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
35
|
+
if (refreshToken)
|
|
36
|
+
try {
|
|
37
|
+
const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
|
|
38
|
+
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
39
|
+
return decoded.id;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/auth/utils/errors.ts
|
|
48
|
+
var import_server2 = require("next/server");
|
|
30
49
|
function handleError(status, message, onError) {
|
|
31
50
|
const res = onError?.({ status, message });
|
|
32
51
|
if (res) return res;
|
|
33
|
-
return new
|
|
52
|
+
return new import_server2.NextResponse(message, { status });
|
|
34
53
|
}
|
|
35
54
|
|
|
36
55
|
// src/auth/email/utils.ts
|
|
@@ -76,8 +95,30 @@ async function verifyCaptcha(token, secret) {
|
|
|
76
95
|
}
|
|
77
96
|
return false;
|
|
78
97
|
}
|
|
98
|
+
var getUserContext = (refreshKey, signingKey, req) => {
|
|
99
|
+
const bearer = req.headers.get("authorization");
|
|
100
|
+
if (!bearer) {
|
|
101
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
102
|
+
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
103
|
+
if (userId) return { refreshUserID: userId };
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const token = bearer.slice(7);
|
|
107
|
+
try {
|
|
108
|
+
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
109
|
+
if (typeof res === "string") {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
accessUserId: res.id
|
|
114
|
+
};
|
|
115
|
+
} catch {
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
};
|
|
79
119
|
// Annotate the CommonJS export names for ESM import in node:
|
|
80
120
|
0 && (module.exports = {
|
|
121
|
+
getUserContext,
|
|
81
122
|
massageRequest,
|
|
82
123
|
verifyCaptcha
|
|
83
124
|
});
|
|
@@ -12,5 +12,9 @@ declare function massageRequest(req: NextRequest, options: InitRoutesOptions): P
|
|
|
12
12
|
};
|
|
13
13
|
}>;
|
|
14
14
|
declare function verifyCaptcha(token: string, secret?: string): Promise<boolean>;
|
|
15
|
+
declare const getUserContext: (refreshKey: string, signingKey: string, req: NextRequest) => {
|
|
16
|
+
refreshUserID?: number;
|
|
17
|
+
accessUserId?: number;
|
|
18
|
+
} | null;
|
|
15
19
|
|
|
16
|
-
export { massageRequest, verifyCaptcha };
|
|
20
|
+
export { getUserContext, massageRequest, verifyCaptcha };
|
|
@@ -12,5 +12,9 @@ declare function massageRequest(req: NextRequest, options: InitRoutesOptions): P
|
|
|
12
12
|
};
|
|
13
13
|
}>;
|
|
14
14
|
declare function verifyCaptcha(token: string, secret?: string): Promise<boolean>;
|
|
15
|
+
declare const getUserContext: (refreshKey: string, signingKey: string, req: NextRequest) => {
|
|
16
|
+
refreshUserID?: number;
|
|
17
|
+
accessUserId?: number;
|
|
18
|
+
} | null;
|
|
15
19
|
|
|
16
|
-
export { massageRequest, verifyCaptcha };
|
|
20
|
+
export { getUserContext, massageRequest, verifyCaptcha };
|
|
@@ -1,9 +1,29 @@
|
|
|
1
|
-
// src/auth/utils
|
|
1
|
+
// src/auth/email/utils.ts
|
|
2
|
+
import { verify as verify2 } from "jsonwebtoken";
|
|
3
|
+
|
|
4
|
+
// src/auth/email/token.ts
|
|
5
|
+
import { compare } from "bcryptjs";
|
|
6
|
+
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
2
7
|
import { NextResponse } from "next/server";
|
|
8
|
+
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
9
|
+
if (refreshToken)
|
|
10
|
+
try {
|
|
11
|
+
const decoded = verify(refreshToken, refreshKey);
|
|
12
|
+
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
13
|
+
return decoded.id;
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/auth/utils/errors.ts
|
|
22
|
+
import { NextResponse as NextResponse2 } from "next/server";
|
|
3
23
|
function handleError(status, message, onError) {
|
|
4
24
|
const res = onError?.({ status, message });
|
|
5
25
|
if (res) return res;
|
|
6
|
-
return new
|
|
26
|
+
return new NextResponse2(message, { status });
|
|
7
27
|
}
|
|
8
28
|
|
|
9
29
|
// src/auth/email/utils.ts
|
|
@@ -49,7 +69,29 @@ async function verifyCaptcha(token, secret) {
|
|
|
49
69
|
}
|
|
50
70
|
return false;
|
|
51
71
|
}
|
|
72
|
+
var getUserContext = (refreshKey, signingKey, req) => {
|
|
73
|
+
const bearer = req.headers.get("authorization");
|
|
74
|
+
if (!bearer) {
|
|
75
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
76
|
+
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
77
|
+
if (userId) return { refreshUserID: userId };
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const token = bearer.slice(7);
|
|
81
|
+
try {
|
|
82
|
+
const res = verify2(token, signingKey);
|
|
83
|
+
if (typeof res === "string") {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
accessUserId: res.id
|
|
88
|
+
};
|
|
89
|
+
} catch {
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
};
|
|
52
93
|
export {
|
|
94
|
+
getUserContext,
|
|
53
95
|
massageRequest,
|
|
54
96
|
verifyCaptcha
|
|
55
97
|
};
|
package/dist/auth/index.cjs.js
CHANGED
|
@@ -24,6 +24,9 @@ __export(auth_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(auth_exports);
|
|
26
26
|
|
|
27
|
+
// src/auth/email/utils.ts
|
|
28
|
+
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
29
|
+
|
|
27
30
|
// src/auth/email/token.ts
|
|
28
31
|
var import_bcryptjs = require("bcryptjs");
|
|
29
32
|
var import_jsonwebtoken = require("jsonwebtoken");
|
|
@@ -74,23 +77,6 @@ function verifyUser(user, password) {
|
|
|
74
77
|
return (0, import_bcryptjs.compare)(password, user.password);
|
|
75
78
|
}
|
|
76
79
|
|
|
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
80
|
// src/auth/utils/errors.ts
|
|
95
81
|
var import_server2 = require("next/server");
|
|
96
82
|
function handleError(status, message, onError) {
|
|
@@ -142,8 +128,44 @@ async function verifyCaptcha(token, secret) {
|
|
|
142
128
|
}
|
|
143
129
|
return false;
|
|
144
130
|
}
|
|
131
|
+
var getUserContext = (refreshKey, signingKey, req) => {
|
|
132
|
+
const bearer = req.headers.get("authorization");
|
|
133
|
+
if (!bearer) {
|
|
134
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
135
|
+
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
136
|
+
if (userId) return { refreshUserID: userId };
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const token = bearer.slice(7);
|
|
140
|
+
try {
|
|
141
|
+
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
142
|
+
if (typeof res === "string") {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
accessUserId: res.id
|
|
147
|
+
};
|
|
148
|
+
} catch {
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// src/auth/email/routes/delete.ts
|
|
154
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
155
|
+
|
|
156
|
+
// src/auth/email/routes/get.ts
|
|
157
|
+
var getGetRoute = (options) => (req) => {
|
|
158
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
159
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
160
|
+
if (userID)
|
|
161
|
+
return getTokenizedResponse(
|
|
162
|
+
generateAccessToken(userID, options.signingKey)
|
|
163
|
+
);
|
|
164
|
+
return getTokenizedResponse();
|
|
165
|
+
};
|
|
145
166
|
|
|
146
167
|
// src/auth/email/routes/post.ts
|
|
168
|
+
var import_bcryptjs2 = require("bcryptjs");
|
|
147
169
|
var getPostRoute = (options) => async (req) => {
|
|
148
170
|
const { data, error } = await massageRequest(req, options);
|
|
149
171
|
if (error || !data) return error;
|
|
@@ -194,7 +216,8 @@ function getEmailAuthRoutes(options) {
|
|
|
194
216
|
GET: getGetRoute(options),
|
|
195
217
|
POST: getPostRoute(options),
|
|
196
218
|
PUT: getPutRoute(options),
|
|
197
|
-
DELETE: getDeleteRoute()
|
|
219
|
+
DELETE: getDeleteRoute(),
|
|
220
|
+
getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
|
|
198
221
|
};
|
|
199
222
|
}
|
|
200
223
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/auth/index.esm.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// src/auth/email/utils.ts
|
|
2
|
+
import { verify as verify2 } from "jsonwebtoken";
|
|
3
|
+
|
|
1
4
|
// src/auth/email/token.ts
|
|
2
5
|
import { compare } from "bcryptjs";
|
|
3
6
|
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
@@ -48,23 +51,6 @@ function verifyUser(user, password) {
|
|
|
48
51
|
return compare(password, user.password);
|
|
49
52
|
}
|
|
50
53
|
|
|
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
54
|
// src/auth/utils/errors.ts
|
|
69
55
|
import { NextResponse as NextResponse2 } from "next/server";
|
|
70
56
|
function handleError(status, message, onError) {
|
|
@@ -116,8 +102,44 @@ async function verifyCaptcha(token, secret) {
|
|
|
116
102
|
}
|
|
117
103
|
return false;
|
|
118
104
|
}
|
|
105
|
+
var getUserContext = (refreshKey, signingKey, req) => {
|
|
106
|
+
const bearer = req.headers.get("authorization");
|
|
107
|
+
if (!bearer) {
|
|
108
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
109
|
+
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
110
|
+
if (userId) return { refreshUserID: userId };
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
const token = bearer.slice(7);
|
|
114
|
+
try {
|
|
115
|
+
const res = verify2(token, signingKey);
|
|
116
|
+
if (typeof res === "string") {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
accessUserId: res.id
|
|
121
|
+
};
|
|
122
|
+
} catch {
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/auth/email/routes/delete.ts
|
|
128
|
+
var getDeleteRoute = () => () => getTokenizedResponse(void 0, "");
|
|
129
|
+
|
|
130
|
+
// src/auth/email/routes/get.ts
|
|
131
|
+
var getGetRoute = (options) => (req) => {
|
|
132
|
+
const refresh = req.cookies.get("refresh")?.value;
|
|
133
|
+
const userID = getUserIdFromRefreshToken(options.refreshKey, refresh);
|
|
134
|
+
if (userID)
|
|
135
|
+
return getTokenizedResponse(
|
|
136
|
+
generateAccessToken(userID, options.signingKey)
|
|
137
|
+
);
|
|
138
|
+
return getTokenizedResponse();
|
|
139
|
+
};
|
|
119
140
|
|
|
120
141
|
// src/auth/email/routes/post.ts
|
|
142
|
+
import { hash } from "bcryptjs";
|
|
121
143
|
var getPostRoute = (options) => async (req) => {
|
|
122
144
|
const { data, error } = await massageRequest(req, options);
|
|
123
145
|
if (error || !data) return error;
|
|
@@ -168,7 +190,8 @@ function getEmailAuthRoutes(options) {
|
|
|
168
190
|
GET: getGetRoute(options),
|
|
169
191
|
POST: getPostRoute(options),
|
|
170
192
|
PUT: getPutRoute(options),
|
|
171
|
-
DELETE: getDeleteRoute()
|
|
193
|
+
DELETE: getDeleteRoute(),
|
|
194
|
+
getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
|
|
172
195
|
};
|
|
173
196
|
}
|
|
174
197
|
export {
|
|
@@ -26,74 +26,24 @@ module.exports = __toCommonJS(graphql_exports);
|
|
|
26
26
|
|
|
27
27
|
// src/graphql/init.ts
|
|
28
28
|
var import_reflect_metadata = require("reflect-metadata");
|
|
29
|
-
var
|
|
29
|
+
var import_server = require("@apollo/server");
|
|
30
30
|
var import_default = require("@apollo/server/plugin/landingPage/default");
|
|
31
31
|
var import_next = require("@as-integrations/next");
|
|
32
32
|
var import_type_graphql = require("type-graphql");
|
|
33
|
-
|
|
34
|
-
// src/graphql/context.ts
|
|
35
|
-
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
36
|
-
|
|
37
|
-
// src/auth/email/token.ts
|
|
38
|
-
var import_bcryptjs = require("bcryptjs");
|
|
39
|
-
var import_jsonwebtoken = require("jsonwebtoken");
|
|
40
|
-
var import_server = require("next/server");
|
|
41
|
-
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
42
|
-
if (refreshToken)
|
|
43
|
-
try {
|
|
44
|
-
const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
|
|
45
|
-
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
46
|
-
return decoded.id;
|
|
47
|
-
} catch (e) {
|
|
48
|
-
if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/auth/utils/errors.ts
|
|
55
|
-
var import_server2 = require("next/server");
|
|
56
|
-
function handleError(status, message, onError) {
|
|
57
|
-
const res = onError?.({ status, message });
|
|
58
|
-
if (res) return res;
|
|
59
|
-
return new import_server2.NextResponse(message, { status });
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// src/graphql/context.ts
|
|
63
|
-
var getContext = (refreshKey, signingKey) => async (req) => {
|
|
64
|
-
const bearer = req.headers.get("authorization");
|
|
65
|
-
const isMobile = req.headers.get("x-platform-is-mobile");
|
|
66
|
-
if (!bearer) {
|
|
67
|
-
if (isMobile) return { userId: null };
|
|
68
|
-
const refresh = req.cookies.get("refresh")?.value;
|
|
69
|
-
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
70
|
-
if (userId) return { userId, onlyQuery: true };
|
|
71
|
-
return { userId: null };
|
|
72
|
-
}
|
|
73
|
-
const token = bearer.slice(7);
|
|
74
|
-
try {
|
|
75
|
-
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
76
|
-
return { userId: typeof res !== "string" ? res.id : null };
|
|
77
|
-
} catch {
|
|
78
|
-
if (isMobile) return handleError(401, "Refresh token is invalid.");
|
|
79
|
-
}
|
|
80
|
-
return { userId: null };
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// src/graphql/init.ts
|
|
84
33
|
async function initGraphQLServer({
|
|
85
34
|
authChecker,
|
|
86
35
|
resolvers,
|
|
87
36
|
plugins,
|
|
88
37
|
refreshKey,
|
|
89
|
-
signingKey
|
|
38
|
+
signingKey,
|
|
39
|
+
context
|
|
90
40
|
}) {
|
|
91
41
|
const { typeDefs, resolvers: builtResolvers } = await (0, import_type_graphql.buildTypeDefsAndResolvers)({
|
|
92
42
|
validate: true,
|
|
93
43
|
authChecker,
|
|
94
44
|
resolvers
|
|
95
45
|
});
|
|
96
|
-
const server = new
|
|
46
|
+
const server = new import_server.ApolloServer({
|
|
97
47
|
typeDefs,
|
|
98
48
|
resolvers: builtResolvers,
|
|
99
49
|
plugins: [
|
|
@@ -112,7 +62,6 @@ async function initGraphQLServer({
|
|
|
112
62
|
introspection: process.env.NODE_ENV !== "production",
|
|
113
63
|
status400ForVariableCoercionErrors: true
|
|
114
64
|
});
|
|
115
|
-
const context = getContext(refreshKey, signingKey);
|
|
116
65
|
const handler = (0, import_next.startServerAndCreateNextHandler)(server, {
|
|
117
66
|
context
|
|
118
67
|
});
|
package/dist/graphql/index.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { initGraphQLServer } from './init.mjs';
|
|
2
2
|
export { AuthorizedContext, Context } from './types.mjs';
|
|
3
3
|
export { FieldLibrary, QueryLibrary, field, query } from './utils.mjs';
|
|
4
|
-
import 'vm';
|
|
5
4
|
import '@apollo/server';
|
|
6
5
|
import 'next/server';
|
|
7
6
|
import 'type-graphql';
|
package/dist/graphql/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { initGraphQLServer } from './init.js';
|
|
2
2
|
export { AuthorizedContext, Context } from './types.js';
|
|
3
3
|
export { FieldLibrary, QueryLibrary, field, query } from './utils.js';
|
|
4
|
-
import 'vm';
|
|
5
4
|
import '@apollo/server';
|
|
6
5
|
import 'next/server';
|
|
7
6
|
import 'type-graphql';
|
|
@@ -9,63 +9,13 @@ import { startServerAndCreateNextHandler } from "@as-integrations/next";
|
|
|
9
9
|
import {
|
|
10
10
|
buildTypeDefsAndResolvers
|
|
11
11
|
} from "type-graphql";
|
|
12
|
-
|
|
13
|
-
// src/graphql/context.ts
|
|
14
|
-
import { verify as verify2 } from "jsonwebtoken";
|
|
15
|
-
|
|
16
|
-
// src/auth/email/token.ts
|
|
17
|
-
import { compare } from "bcryptjs";
|
|
18
|
-
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
19
|
-
import { NextResponse } from "next/server";
|
|
20
|
-
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
21
|
-
if (refreshToken)
|
|
22
|
-
try {
|
|
23
|
-
const decoded = verify(refreshToken, refreshKey);
|
|
24
|
-
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
25
|
-
return decoded.id;
|
|
26
|
-
} catch (e) {
|
|
27
|
-
if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/auth/utils/errors.ts
|
|
34
|
-
import { NextResponse as NextResponse2 } from "next/server";
|
|
35
|
-
function handleError(status, message, onError) {
|
|
36
|
-
const res = onError?.({ status, message });
|
|
37
|
-
if (res) return res;
|
|
38
|
-
return new NextResponse2(message, { status });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/graphql/context.ts
|
|
42
|
-
var getContext = (refreshKey, signingKey) => async (req) => {
|
|
43
|
-
const bearer = req.headers.get("authorization");
|
|
44
|
-
const isMobile = req.headers.get("x-platform-is-mobile");
|
|
45
|
-
if (!bearer) {
|
|
46
|
-
if (isMobile) return { userId: null };
|
|
47
|
-
const refresh = req.cookies.get("refresh")?.value;
|
|
48
|
-
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
49
|
-
if (userId) return { userId, onlyQuery: true };
|
|
50
|
-
return { userId: null };
|
|
51
|
-
}
|
|
52
|
-
const token = bearer.slice(7);
|
|
53
|
-
try {
|
|
54
|
-
const res = verify2(token, signingKey);
|
|
55
|
-
return { userId: typeof res !== "string" ? res.id : null };
|
|
56
|
-
} catch {
|
|
57
|
-
if (isMobile) return handleError(401, "Refresh token is invalid.");
|
|
58
|
-
}
|
|
59
|
-
return { userId: null };
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// src/graphql/init.ts
|
|
63
12
|
async function initGraphQLServer({
|
|
64
13
|
authChecker,
|
|
65
14
|
resolvers,
|
|
66
15
|
plugins,
|
|
67
16
|
refreshKey,
|
|
68
|
-
signingKey
|
|
17
|
+
signingKey,
|
|
18
|
+
context
|
|
69
19
|
}) {
|
|
70
20
|
const { typeDefs, resolvers: builtResolvers } = await buildTypeDefsAndResolvers({
|
|
71
21
|
validate: true,
|
|
@@ -91,7 +41,6 @@ async function initGraphQLServer({
|
|
|
91
41
|
introspection: process.env.NODE_ENV !== "production",
|
|
92
42
|
status400ForVariableCoercionErrors: true
|
|
93
43
|
});
|
|
94
|
-
const context = getContext(refreshKey, signingKey);
|
|
95
44
|
const handler = startServerAndCreateNextHandler(server, {
|
|
96
45
|
context
|
|
97
46
|
});
|
package/dist/graphql/init.cjs.js
CHANGED
|
@@ -24,74 +24,24 @@ __export(init_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(init_exports);
|
|
26
26
|
var import_reflect_metadata = require("reflect-metadata");
|
|
27
|
-
var
|
|
27
|
+
var import_server = require("@apollo/server");
|
|
28
28
|
var import_default = require("@apollo/server/plugin/landingPage/default");
|
|
29
29
|
var import_next = require("@as-integrations/next");
|
|
30
30
|
var import_type_graphql = require("type-graphql");
|
|
31
|
-
|
|
32
|
-
// src/graphql/context.ts
|
|
33
|
-
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
34
|
-
|
|
35
|
-
// src/auth/email/token.ts
|
|
36
|
-
var import_bcryptjs = require("bcryptjs");
|
|
37
|
-
var import_jsonwebtoken = require("jsonwebtoken");
|
|
38
|
-
var import_server = require("next/server");
|
|
39
|
-
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
40
|
-
if (refreshToken)
|
|
41
|
-
try {
|
|
42
|
-
const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
|
|
43
|
-
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
44
|
-
return decoded.id;
|
|
45
|
-
} catch (e) {
|
|
46
|
-
if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/auth/utils/errors.ts
|
|
53
|
-
var import_server2 = require("next/server");
|
|
54
|
-
function handleError(status, message, onError) {
|
|
55
|
-
const res = onError?.({ status, message });
|
|
56
|
-
if (res) return res;
|
|
57
|
-
return new import_server2.NextResponse(message, { status });
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/graphql/context.ts
|
|
61
|
-
var getContext = (refreshKey, signingKey) => async (req) => {
|
|
62
|
-
const bearer = req.headers.get("authorization");
|
|
63
|
-
const isMobile = req.headers.get("x-platform-is-mobile");
|
|
64
|
-
if (!bearer) {
|
|
65
|
-
if (isMobile) return { userId: null };
|
|
66
|
-
const refresh = req.cookies.get("refresh")?.value;
|
|
67
|
-
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
68
|
-
if (userId) return { userId, onlyQuery: true };
|
|
69
|
-
return { userId: null };
|
|
70
|
-
}
|
|
71
|
-
const token = bearer.slice(7);
|
|
72
|
-
try {
|
|
73
|
-
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
74
|
-
return { userId: typeof res !== "string" ? res.id : null };
|
|
75
|
-
} catch {
|
|
76
|
-
if (isMobile) return handleError(401, "Refresh token is invalid.");
|
|
77
|
-
}
|
|
78
|
-
return { userId: null };
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// src/graphql/init.ts
|
|
82
31
|
async function initGraphQLServer({
|
|
83
32
|
authChecker,
|
|
84
33
|
resolvers,
|
|
85
34
|
plugins,
|
|
86
35
|
refreshKey,
|
|
87
|
-
signingKey
|
|
36
|
+
signingKey,
|
|
37
|
+
context
|
|
88
38
|
}) {
|
|
89
39
|
const { typeDefs, resolvers: builtResolvers } = await (0, import_type_graphql.buildTypeDefsAndResolvers)({
|
|
90
40
|
validate: true,
|
|
91
41
|
authChecker,
|
|
92
42
|
resolvers
|
|
93
43
|
});
|
|
94
|
-
const server = new
|
|
44
|
+
const server = new import_server.ApolloServer({
|
|
95
45
|
typeDefs,
|
|
96
46
|
resolvers: builtResolvers,
|
|
97
47
|
plugins: [
|
|
@@ -110,7 +60,6 @@ async function initGraphQLServer({
|
|
|
110
60
|
introspection: process.env.NODE_ENV !== "production",
|
|
111
61
|
status400ForVariableCoercionErrors: true
|
|
112
62
|
});
|
|
113
|
-
const context = getContext(refreshKey, signingKey);
|
|
114
63
|
const handler = (0, import_next.startServerAndCreateNextHandler)(server, {
|
|
115
64
|
context
|
|
116
65
|
});
|
package/dist/graphql/init.d.mts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import * as vm from 'vm';
|
|
2
1
|
import { ApolloServerPlugin } from '@apollo/server';
|
|
3
2
|
import { NextRequest } from 'next/server';
|
|
4
3
|
import { AuthChecker, NonEmptyArray } from 'type-graphql';
|
|
5
4
|
|
|
6
|
-
declare function initGraphQLServer({ authChecker, resolvers, plugins, refreshKey, signingKey, }: {
|
|
5
|
+
declare function initGraphQLServer({ authChecker, resolvers, plugins, refreshKey, signingKey, context, }: {
|
|
7
6
|
authChecker?: AuthChecker<any>;
|
|
8
7
|
resolvers: NonEmptyArray<Function>;
|
|
9
8
|
plugins?: ApolloServerPlugin[];
|
|
9
|
+
context?: (req: NextRequest) => Promise<any>;
|
|
10
10
|
refreshKey: string;
|
|
11
11
|
signingKey: string;
|
|
12
12
|
}): Promise<{
|
|
13
13
|
GET: (request: NextRequest) => Promise<Response>;
|
|
14
14
|
POST: (request: NextRequest) => Promise<Response>;
|
|
15
|
-
context: (req: NextRequest) => Promise<
|
|
15
|
+
context: ((req: NextRequest) => Promise<any>) | undefined;
|
|
16
16
|
}>;
|
|
17
17
|
|
|
18
18
|
export { initGraphQLServer };
|
package/dist/graphql/init.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import * as vm from 'vm';
|
|
2
1
|
import { ApolloServerPlugin } from '@apollo/server';
|
|
3
2
|
import { NextRequest } from 'next/server';
|
|
4
3
|
import { AuthChecker, NonEmptyArray } from 'type-graphql';
|
|
5
4
|
|
|
6
|
-
declare function initGraphQLServer({ authChecker, resolvers, plugins, refreshKey, signingKey, }: {
|
|
5
|
+
declare function initGraphQLServer({ authChecker, resolvers, plugins, refreshKey, signingKey, context, }: {
|
|
7
6
|
authChecker?: AuthChecker<any>;
|
|
8
7
|
resolvers: NonEmptyArray<Function>;
|
|
9
8
|
plugins?: ApolloServerPlugin[];
|
|
9
|
+
context?: (req: NextRequest) => Promise<any>;
|
|
10
10
|
refreshKey: string;
|
|
11
11
|
signingKey: string;
|
|
12
12
|
}): Promise<{
|
|
13
13
|
GET: (request: NextRequest) => Promise<Response>;
|
|
14
14
|
POST: (request: NextRequest) => Promise<Response>;
|
|
15
|
-
context: (req: NextRequest) => Promise<
|
|
15
|
+
context: ((req: NextRequest) => Promise<any>) | undefined;
|
|
16
16
|
}>;
|
|
17
17
|
|
|
18
18
|
export { initGraphQLServer };
|
package/dist/graphql/init.esm.js
CHANGED
|
@@ -9,63 +9,13 @@ import { startServerAndCreateNextHandler } from "@as-integrations/next";
|
|
|
9
9
|
import {
|
|
10
10
|
buildTypeDefsAndResolvers
|
|
11
11
|
} from "type-graphql";
|
|
12
|
-
|
|
13
|
-
// src/graphql/context.ts
|
|
14
|
-
import { verify as verify2 } from "jsonwebtoken";
|
|
15
|
-
|
|
16
|
-
// src/auth/email/token.ts
|
|
17
|
-
import { compare } from "bcryptjs";
|
|
18
|
-
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
19
|
-
import { NextResponse } from "next/server";
|
|
20
|
-
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
21
|
-
if (refreshToken)
|
|
22
|
-
try {
|
|
23
|
-
const decoded = verify(refreshToken, refreshKey);
|
|
24
|
-
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
25
|
-
return decoded.id;
|
|
26
|
-
} catch (e) {
|
|
27
|
-
if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/auth/utils/errors.ts
|
|
34
|
-
import { NextResponse as NextResponse2 } from "next/server";
|
|
35
|
-
function handleError(status, message, onError) {
|
|
36
|
-
const res = onError?.({ status, message });
|
|
37
|
-
if (res) return res;
|
|
38
|
-
return new NextResponse2(message, { status });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/graphql/context.ts
|
|
42
|
-
var getContext = (refreshKey, signingKey) => async (req) => {
|
|
43
|
-
const bearer = req.headers.get("authorization");
|
|
44
|
-
const isMobile = req.headers.get("x-platform-is-mobile");
|
|
45
|
-
if (!bearer) {
|
|
46
|
-
if (isMobile) return { userId: null };
|
|
47
|
-
const refresh = req.cookies.get("refresh")?.value;
|
|
48
|
-
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
49
|
-
if (userId) return { userId, onlyQuery: true };
|
|
50
|
-
return { userId: null };
|
|
51
|
-
}
|
|
52
|
-
const token = bearer.slice(7);
|
|
53
|
-
try {
|
|
54
|
-
const res = verify2(token, signingKey);
|
|
55
|
-
return { userId: typeof res !== "string" ? res.id : null };
|
|
56
|
-
} catch {
|
|
57
|
-
if (isMobile) return handleError(401, "Refresh token is invalid.");
|
|
58
|
-
}
|
|
59
|
-
return { userId: null };
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// src/graphql/init.ts
|
|
63
12
|
async function initGraphQLServer({
|
|
64
13
|
authChecker,
|
|
65
14
|
resolvers,
|
|
66
15
|
plugins,
|
|
67
16
|
refreshKey,
|
|
68
|
-
signingKey
|
|
17
|
+
signingKey,
|
|
18
|
+
context
|
|
69
19
|
}) {
|
|
70
20
|
const { typeDefs, resolvers: builtResolvers } = await buildTypeDefsAndResolvers({
|
|
71
21
|
validate: true,
|
|
@@ -91,7 +41,6 @@ async function initGraphQLServer({
|
|
|
91
41
|
introspection: process.env.NODE_ENV !== "production",
|
|
92
42
|
status400ForVariableCoercionErrors: true
|
|
93
43
|
});
|
|
94
|
-
const context = getContext(refreshKey, signingKey);
|
|
95
44
|
const handler = startServerAndCreateNextHandler(server, {
|
|
96
45
|
context
|
|
97
46
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "naystack",
|
|
3
|
-
"version": "1.1.12-beta.
|
|
3
|
+
"version": "1.1.12-beta.4",
|
|
4
4
|
"description": "A stack built with tight Next + Drizzle + GraphQL",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"bcryptjs": "^3.0.2",
|
|
56
56
|
"drizzle-orm": "^0.44.5",
|
|
57
57
|
"jsonwebtoken": "^9.0.2",
|
|
58
|
-
"next": "^15.
|
|
58
|
+
"next": "^15.6.0-canary.29",
|
|
59
59
|
"reflect-metadata": "^0.2.2",
|
|
60
60
|
"type-graphql": "2.0.0-rc.2"
|
|
61
61
|
},
|
|
@@ -1,76 +0,0 @@
|
|
|
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/graphql/context.ts
|
|
21
|
-
var context_exports = {};
|
|
22
|
-
__export(context_exports, {
|
|
23
|
-
getContext: () => getContext
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(context_exports);
|
|
26
|
-
var import_jsonwebtoken2 = require("jsonwebtoken");
|
|
27
|
-
|
|
28
|
-
// src/auth/email/token.ts
|
|
29
|
-
var import_bcryptjs = require("bcryptjs");
|
|
30
|
-
var import_jsonwebtoken = require("jsonwebtoken");
|
|
31
|
-
var import_server = require("next/server");
|
|
32
|
-
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
33
|
-
if (refreshToken)
|
|
34
|
-
try {
|
|
35
|
-
const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
|
|
36
|
-
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
37
|
-
return decoded.id;
|
|
38
|
-
} catch (e) {
|
|
39
|
-
if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// src/auth/utils/errors.ts
|
|
46
|
-
var import_server2 = require("next/server");
|
|
47
|
-
function handleError(status, message, onError) {
|
|
48
|
-
const res = onError?.({ status, message });
|
|
49
|
-
if (res) return res;
|
|
50
|
-
return new import_server2.NextResponse(message, { status });
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// src/graphql/context.ts
|
|
54
|
-
var getContext = (refreshKey, signingKey) => async (req) => {
|
|
55
|
-
const bearer = req.headers.get("authorization");
|
|
56
|
-
const isMobile = req.headers.get("x-platform-is-mobile");
|
|
57
|
-
if (!bearer) {
|
|
58
|
-
if (isMobile) return { userId: null };
|
|
59
|
-
const refresh = req.cookies.get("refresh")?.value;
|
|
60
|
-
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
61
|
-
if (userId) return { userId, onlyQuery: true };
|
|
62
|
-
return { userId: null };
|
|
63
|
-
}
|
|
64
|
-
const token = bearer.slice(7);
|
|
65
|
-
try {
|
|
66
|
-
const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
|
|
67
|
-
return { userId: typeof res !== "string" ? res.id : null };
|
|
68
|
-
} catch {
|
|
69
|
-
if (isMobile) return handleError(401, "Refresh token is invalid.");
|
|
70
|
-
}
|
|
71
|
-
return { userId: null };
|
|
72
|
-
};
|
|
73
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
-
0 && (module.exports = {
|
|
75
|
-
getContext
|
|
76
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
// src/graphql/context.ts
|
|
2
|
-
import { verify as verify2 } from "jsonwebtoken";
|
|
3
|
-
|
|
4
|
-
// src/auth/email/token.ts
|
|
5
|
-
import { compare } from "bcryptjs";
|
|
6
|
-
import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
|
|
7
|
-
import { NextResponse } from "next/server";
|
|
8
|
-
function getUserIdFromRefreshToken(refreshKey, refreshToken) {
|
|
9
|
-
if (refreshToken)
|
|
10
|
-
try {
|
|
11
|
-
const decoded = verify(refreshToken, refreshKey);
|
|
12
|
-
if (typeof decoded !== "string" && typeof decoded.id === "number")
|
|
13
|
-
return decoded.id;
|
|
14
|
-
} catch (e) {
|
|
15
|
-
if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// src/auth/utils/errors.ts
|
|
22
|
-
import { NextResponse as NextResponse2 } from "next/server";
|
|
23
|
-
function handleError(status, message, onError) {
|
|
24
|
-
const res = onError?.({ status, message });
|
|
25
|
-
if (res) return res;
|
|
26
|
-
return new NextResponse2(message, { status });
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// src/graphql/context.ts
|
|
30
|
-
var getContext = (refreshKey, signingKey) => async (req) => {
|
|
31
|
-
const bearer = req.headers.get("authorization");
|
|
32
|
-
const isMobile = req.headers.get("x-platform-is-mobile");
|
|
33
|
-
if (!bearer) {
|
|
34
|
-
if (isMobile) return { userId: null };
|
|
35
|
-
const refresh = req.cookies.get("refresh")?.value;
|
|
36
|
-
const userId = getUserIdFromRefreshToken(refreshKey, refresh);
|
|
37
|
-
if (userId) return { userId, onlyQuery: true };
|
|
38
|
-
return { userId: null };
|
|
39
|
-
}
|
|
40
|
-
const token = bearer.slice(7);
|
|
41
|
-
try {
|
|
42
|
-
const res = verify2(token, signingKey);
|
|
43
|
-
return { userId: typeof res !== "string" ? res.id : null };
|
|
44
|
-
} catch {
|
|
45
|
-
if (isMobile) return handleError(401, "Refresh token is invalid.");
|
|
46
|
-
}
|
|
47
|
-
return { userId: null };
|
|
48
|
-
};
|
|
49
|
-
export {
|
|
50
|
-
getContext
|
|
51
|
-
};
|