Public
Back
Version 3
3/5/2024
import { getUser, verifyPassword } from "https://esm.town/v/pomdtr/lucia_demo";
type BasicAuthOptions = {
user: string;
};
export function basicAuth(next: (Request) => Response | Promise<Response>, options: BasicAuthOptions) {
return async (req: Request) => {
if (req.headers.get("referer") == "https://www.val.town/") {
return new Response(
`Basic Auth is disabled in Val Town iframes.
<a href="/" target="blank_">Open in a new tab.</a>`,
{
status: 400,
headers: {
"Content-type": "text/html",
},
},
);
}
const isAuth = await isRequestAuthenticated(req);
if (!isAuth) {
return new Response("Unauthorized", {
status: 401,
headers: {
"WWW-Authenticate": "Basic",
},
});
}
return next(req);
};
}
function extractCredentials(authorization): { username: string; password: string } {
const parts = authorization.split(" ");
Updated: March 5, 2024