simple-fs-server/routes/login.tsx

96 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-02-08 01:33:54 +09:00
import { Head, asset } from "$fresh/runtime.ts";
2023-01-14 03:03:22 +09:00
import { HandlerContext, PageProps } from "$fresh/server.ts";
import { setCookie } from "http/cookie.ts";
import { Status } from "http/http_status.ts";
import { connectDB } from "../src/user/db.ts";
import { getUser, verifyUser } from "../src/user/user.ts";
import { create as createJWT } from "djwt";
import { prepareSecretKey } from "../util/secret.ts";
import LoginForm from "../islands/Login.tsx";
2023-01-05 18:18:07 +09:00
2023-01-14 03:03:58 +09:00
async function GET(_req: Request, ctx: HandlerContext) {
2023-01-14 03:03:22 +09:00
return await ctx.render();
}
async function POST(req: Request, _ctx: HandlerContext): Promise<Response> {
const url = new URL(req.url);
const form = await req.formData();
const username = form.get("username");
const password = form.get("password");
if (username && password) {
const DB = await connectDB();
const user = await getUser(DB, username.toString());
if (user) {
const SECRET_KEY = await prepareSecretKey();
if (await verifyUser(user, password.toString())) {
const headers = new Headers();
const jwt = await createJWT({ alg: "HS512", typ: "JWT" }, {
username: user.name,
}, SECRET_KEY);
setCookie(headers, {
name: "auth",
value: jwt,
httpOnly: true,
sameSite: "Strict",
maxAge: 60 * 60 * 24 * 7,
domain: url.hostname,
path: "/",
secure: url.protocol === "https:",
});
2023-01-14 03:03:58 +09:00
2023-01-14 03:03:22 +09:00
let redirect = "/";
if (url.searchParams.has("redirect")) {
redirect = url.searchParams.get("redirect")!;
}
headers.set("Location", redirect);
return new Response(null, {
status: Status.SeeOther, // See Other
headers: headers,
});
}
}
}
return new Response(
`<!DOCTYPE html><html>
<head> <title> Login Failed </title> </head>
<body>
<h1> Login Failed </h1>
<p> <a href="/"> Back to Home </a> </p>
<script>
2023-01-14 03:03:58 +09:00
document.location.href = "/login?failed=true&redirect=${
url.searchParams.get("redirect")
}";
2023-01-14 03:03:22 +09:00
</script>
</body>
</html>`,
{
headers: {
"Content-Type": "text/html",
},
status: Status.Unauthorized,
},
);
}
export const handler = {
GET,
POST,
};
export default function Login(props: PageProps) {
const redirect = props.url.searchParams.get("redirect");
const failed = props.url.searchParams.get("failed") === "true";
2023-01-06 18:24:27 +09:00
return (
<>
<Head>
<title>Simple file server - Login</title>
2023-02-08 01:33:54 +09:00
<link rel="stylesheet" href={asset("/base.css")} />
2023-01-06 18:24:27 +09:00
</Head>
<div class="">
2023-01-14 03:03:58 +09:00
<LoginForm redirect={redirect ?? "/"} failed={failed} />
2023-01-06 18:24:27 +09:00
</div>
</>
);
2023-01-05 18:18:07 +09:00
}