mirror of
https://github.com/BradNut/weddingsite
synced 2025-09-08 17:40:36 +00:00
82 lines
1.7 KiB
TypeScript
82 lines
1.7 KiB
TypeScript
import { auth } from "@/auth/lucia";
|
|
import * as context from "next/headers";
|
|
import { NextResponse } from "next/server";
|
|
import { LuciaError } from "lucia";
|
|
|
|
import type { NextRequest } from "next/server";
|
|
|
|
export const POST = async (request: NextRequest) => {
|
|
const formData = await request.formData();
|
|
const username = formData.get("username");
|
|
const password = formData.get("password");
|
|
// basic check
|
|
if (
|
|
typeof username !== "string" ||
|
|
username.length < 1 ||
|
|
username.length > 31
|
|
) {
|
|
return NextResponse.json(
|
|
{
|
|
error: "Invalid username",
|
|
},
|
|
{
|
|
status: 400,
|
|
}
|
|
);
|
|
}
|
|
if (
|
|
typeof password !== "string" ||
|
|
password.length < 1 ||
|
|
password.length > 255
|
|
) {
|
|
return NextResponse.json(
|
|
{
|
|
error: "Invalid password",
|
|
},
|
|
{
|
|
status: 400,
|
|
}
|
|
);
|
|
}
|
|
try {
|
|
// find user by key
|
|
// and validate password
|
|
const key = await auth.useKey("username", username.toLowerCase(), password);
|
|
const session = await auth.createSession({
|
|
userId: key.userId,
|
|
attributes: {},
|
|
});
|
|
const authRequest = auth.handleRequest(request.method, context);
|
|
authRequest.setSession(session);
|
|
return new Response(null, {
|
|
status: 302,
|
|
headers: {
|
|
Location: "/", // redirect to profile page
|
|
},
|
|
});
|
|
} catch (e) {
|
|
if (
|
|
e instanceof LuciaError &&
|
|
(e.message === "AUTH_INVALID_KEY_ID" ||
|
|
e.message === "AUTH_INVALID_PASSWORD")
|
|
) {
|
|
// user does not exist or invalid password
|
|
return NextResponse.json(
|
|
{
|
|
error: "Incorrect username or password",
|
|
},
|
|
{
|
|
status: 400,
|
|
}
|
|
);
|
|
}
|
|
return NextResponse.json(
|
|
{
|
|
error: "An unknown error occurred",
|
|
},
|
|
{
|
|
status: 500,
|
|
}
|
|
);
|
|
}
|
|
};
|