release: WRNexusJS 0.5.0
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { authSession, requireAuth } from "@wrnexus/auth";
|
||||
import type { Middleware } from "@wrnexus/core";
|
||||
import { auth } from "../lib/auth.ts";
|
||||
|
||||
const hydrate = authSession(auth);
|
||||
const accountGuard = requireAuth({ loginPath: "/sign-in" });
|
||||
|
||||
const middleware: Middleware = async (ctx, next) => {
|
||||
return hydrate(ctx, () =>
|
||||
ctx.url.pathname.startsWith("/account") ? accountGuard(ctx, next) : next(),
|
||||
);
|
||||
};
|
||||
|
||||
export default middleware;
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { Context, Next } from "@wrnexus/core";
|
||||
import { captchaGuard } from "@wrnexus/captcha/server";
|
||||
import { captchaEngine } from "../lib/captcha.ts";
|
||||
|
||||
const protectLogin = captchaGuard({
|
||||
action: "auth-login",
|
||||
engine: captchaEngine,
|
||||
responseField: "wrn-captcha-response",
|
||||
bindHostname: true,
|
||||
bindSession: true,
|
||||
verifiedForMs: 5 * 60_000,
|
||||
onFailure(_ctx, result) {
|
||||
return Response.json(
|
||||
{
|
||||
ok: false,
|
||||
code: result.code ?? "captcha-invalid",
|
||||
message: result.message ?? "Complete the security check below before signing in.",
|
||||
},
|
||||
{
|
||||
status: 403,
|
||||
headers: { "cache-control": "no-store" },
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export default function captchaLogin(ctx: Context, next: Next) {
|
||||
return ctx.req.method === "POST" && ctx.url.pathname === "/api/auth/login"
|
||||
? protectLogin(ctx, next)
|
||||
: next();
|
||||
}
|
||||
Reference in New Issue
Block a user