@tylix/core 0.4.0 → 0.4.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/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { requireAuth } from "../http/requireAuth.js";
|
|
2
2
|
|
|
3
|
-
export function registerFeatureRoutes(router, features, { secret } = {}) {
|
|
3
|
+
export function registerFeatureRoutes(router, features, { secret, hydrate } = {}) {
|
|
4
4
|
for (const { manifest, controller } of features) {
|
|
5
5
|
const base = `/api/${manifest.table}`;
|
|
6
|
-
const wrap = (fn) => (manifest.auth ? requireAuth(fn, secret) : fn);
|
|
6
|
+
const wrap = (fn) => (manifest.auth ? requireAuth(fn, secret, { hydrate }) : fn);
|
|
7
7
|
|
|
8
8
|
router.get(base, wrap((req, res) => controller.index(req, res)));
|
|
9
9
|
router.get(`${base}/:id`, wrap((req, res) => controller.show(req, res)));
|
|
@@ -11,4 +11,4 @@ export function registerFeatureRoutes(router, features, { secret } = {}) {
|
|
|
11
11
|
router.put(`${base}/:id`, wrap((req, res) => controller.update(req, res)));
|
|
12
12
|
router.delete(`${base}/:id`, wrap((req, res) => controller.destroy(req, res)));
|
|
13
13
|
}
|
|
14
|
-
}
|
|
14
|
+
}
|
|
@@ -22,9 +22,10 @@ async function collect(dir) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Mounts every app/routes/**/*.{js,ts} file exporting routes(router, { auth }).
|
|
25
|
-
|
|
25
|
+
// `hydrate` (optional) is passed to requireAuth: see http/requireAuth.js.
|
|
26
|
+
export async function registerRouteFiles(router, baseDir, { secret, hydrate } = {}) {
|
|
26
27
|
const routesDir = path.join(baseDir, 'app', 'routes')
|
|
27
|
-
const context = { auth: (handler) => requireAuth(handler, secret) }
|
|
28
|
+
const context = { auth: (handler) => requireAuth(handler, secret, { hydrate }) }
|
|
28
29
|
const mounted = []
|
|
29
30
|
|
|
30
31
|
for (const file of await collect(routesDir)) {
|
package/src/http/requireAuth.js
CHANGED
|
@@ -8,8 +8,16 @@ import { verifyToken, ACCESS_TOKEN_COOKIE } from "@tylix/auth";
|
|
|
8
8
|
* that can't rely on cookies). On success, req.user is set to the
|
|
9
9
|
* token's payload (e.g. { userId }); on failure, responds 401 and
|
|
10
10
|
* never calls the wrapped handler.
|
|
11
|
+
*
|
|
12
|
+
* options.hydrate(payload, req) is optional. It runs after the token is
|
|
13
|
+
* verified and can add fields to req.user (for example a team id):
|
|
14
|
+
* - return an object -> its fields are merged into req.user
|
|
15
|
+
* - return null/false -> the request is rejected with 401
|
|
16
|
+
* - throw -> the request is rejected with 500
|
|
11
17
|
*/
|
|
12
|
-
export function requireAuth(handler, secret) {
|
|
18
|
+
export function requireAuth(handler, secret, options = {}) {
|
|
19
|
+
const { hydrate } = options;
|
|
20
|
+
|
|
13
21
|
return async (req, res) => {
|
|
14
22
|
const cookieToken = req.cookies?.[ACCESS_TOKEN_COOKIE];
|
|
15
23
|
|
|
@@ -29,6 +37,24 @@ export function requireAuth(handler, secret) {
|
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
req.user = result.payload;
|
|
40
|
+
|
|
41
|
+
if (typeof hydrate === "function") {
|
|
42
|
+
let extra;
|
|
43
|
+
try {
|
|
44
|
+
extra = await hydrate(result.payload, req);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error("[auth] hydrate failed:", error);
|
|
47
|
+
return res.status(500).json({ error: "Couldn't load your account." });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (extra === null || extra === false) {
|
|
51
|
+
return res.status(401).json({ error: "Not authenticated." });
|
|
52
|
+
}
|
|
53
|
+
if (extra && typeof extra === "object") {
|
|
54
|
+
req.user = { ...result.payload, ...extra };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
32
58
|
return handler(req, res);
|
|
33
59
|
};
|
|
34
|
-
}
|
|
60
|
+
}
|