Cleanup middleware
This commit is contained in:
parent
c97c3f7b69
commit
ac44a249ed
|
@ -3,9 +3,9 @@ import { deleteCookie, getCookies } from "$std/http/cookie.ts";
|
||||||
import { getUserFromNonExpiredLoginToken } from "@/db/mod.ts";
|
import { getUserFromNonExpiredLoginToken } from "@/db/mod.ts";
|
||||||
import { type ContextState } from "@/types.ts";
|
import { type ContextState } from "@/types.ts";
|
||||||
|
|
||||||
export async function handler(
|
async function currentUser(
|
||||||
request: Request,
|
request: Request,
|
||||||
ctx: MiddlewareHandlerContext<ContextState>,
|
context: MiddlewareHandlerContext<ContextState>,
|
||||||
) {
|
) {
|
||||||
let hasBadAuthCookie = false;
|
let hasBadAuthCookie = false;
|
||||||
const { lsauth } = getCookies(request.headers);
|
const { lsauth } = getCookies(request.headers);
|
||||||
|
@ -14,17 +14,31 @@ export async function handler(
|
||||||
const user = await getUserFromNonExpiredLoginToken(lsauth);
|
const user = await getUserFromNonExpiredLoginToken(lsauth);
|
||||||
if (!user) hasBadAuthCookie = true;
|
if (!user) hasBadAuthCookie = true;
|
||||||
else {
|
else {
|
||||||
ctx.state.user = user;
|
context.state.user = user;
|
||||||
delete ctx.state.user.createdAt;
|
delete context.state.user.createdAt;
|
||||||
delete ctx.state.user.updatedAt;
|
delete context.state.user.updatedAt;
|
||||||
delete ctx.state.user.passwordDigest;
|
delete context.state.user.passwordDigest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const resp = await context.next();
|
||||||
const resp = await ctx.next();
|
|
||||||
if (resp) {
|
if (resp) {
|
||||||
resp.headers.set("server", "fresh server");
|
|
||||||
if (hasBadAuthCookie) deleteCookie(resp.headers, "lsauth");
|
if (hasBadAuthCookie) deleteCookie(resp.headers, "lsauth");
|
||||||
}
|
}
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function serverHeader(
|
||||||
|
_request: Request,
|
||||||
|
context: MiddlewareHandlerContext<ContextState>,
|
||||||
|
) {
|
||||||
|
const resp = await context.next();
|
||||||
|
if (resp) {
|
||||||
|
resp.headers.set("server", "fresh server");
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const handler = [
|
||||||
|
currentUser,
|
||||||
|
serverHeader,
|
||||||
|
];
|
||||||
|
|
Loading…
Reference in a new issue