weifuwu 0.22.0 → 0.22.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/index.js +31 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3428,17 +3428,30 @@ function user(options) {
|
|
|
3428
3428
|
}
|
|
3429
3429
|
function middleware() {
|
|
3430
3430
|
return async (req, ctx, next) => {
|
|
3431
|
-
const
|
|
3432
|
-
if (
|
|
3433
|
-
|
|
3431
|
+
const sessionUserId = ctx.session?.userId;
|
|
3432
|
+
if (sessionUserId) {
|
|
3433
|
+
const row = await findById(sessionUserId);
|
|
3434
|
+
if (row) {
|
|
3435
|
+
ctx.user = stripPassword(row);
|
|
3436
|
+
return next(req, ctx);
|
|
3437
|
+
}
|
|
3438
|
+
if (typeof ctx.session?.destroy === "function") {
|
|
3439
|
+
;
|
|
3440
|
+
ctx.session.destroy();
|
|
3441
|
+
} else {
|
|
3442
|
+
delete ctx.session?.userId;
|
|
3443
|
+
}
|
|
3434
3444
|
}
|
|
3435
|
-
const
|
|
3436
|
-
const
|
|
3437
|
-
if (
|
|
3438
|
-
|
|
3445
|
+
const header = req.headers.get("Authorization");
|
|
3446
|
+
const token = header?.startsWith("Bearer ") ? header.slice(7) : null;
|
|
3447
|
+
if (token) {
|
|
3448
|
+
const userData = await verify(token);
|
|
3449
|
+
if (userData) {
|
|
3450
|
+
ctx.user = userData;
|
|
3451
|
+
return next(req, ctx);
|
|
3452
|
+
}
|
|
3439
3453
|
}
|
|
3440
|
-
|
|
3441
|
-
return next(req, ctx);
|
|
3454
|
+
return new Response("Unauthorized", { status: 401, headers: { "WWW-Authenticate": "Bearer" } });
|
|
3442
3455
|
};
|
|
3443
3456
|
}
|
|
3444
3457
|
function router() {
|
|
@@ -3456,12 +3469,19 @@ function user(options) {
|
|
|
3456
3469
|
return Response.json({ error: err.message }, { status });
|
|
3457
3470
|
}
|
|
3458
3471
|
});
|
|
3459
|
-
r2.post("/login", async (req) => {
|
|
3472
|
+
r2.post("/login", async (req, ctx) => {
|
|
3460
3473
|
try {
|
|
3461
3474
|
const body = await req.json();
|
|
3462
3475
|
const result = await login(body);
|
|
3476
|
+
if (ctx.session) {
|
|
3477
|
+
;
|
|
3478
|
+
ctx.session.userId = result.user.id;
|
|
3479
|
+
ctx.session.role = result.user.role;
|
|
3480
|
+
}
|
|
3463
3481
|
const res = Response.json(result);
|
|
3464
|
-
|
|
3482
|
+
if (!ctx.session) {
|
|
3483
|
+
res.headers.set("Set-Cookie", `session=${result.token}; HttpOnly; SameSite=Lax; Path=/`);
|
|
3484
|
+
}
|
|
3465
3485
|
return res;
|
|
3466
3486
|
} catch (err) {
|
|
3467
3487
|
if (err instanceof z2.ZodError) {
|