9d32552457
Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents for deployment to Coolify: in-container cron scheduler (scripts/cron-runner.mjs) and disk-based upload storage served via /api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health for container health checks. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { requireUser } from "@/lib/auth/user";
|
|
import { createSessionToken, COOKIE_NAME, SESSION_DURATION_SECONDS } from "@/lib/auth/session";
|
|
import { hashPassword, passwordProblem, verifyPassword } from "@/lib/auth/password";
|
|
|
|
export async function POST(req: Request) {
|
|
const auth = await requireUser();
|
|
if (!auth.ok) return auth.response;
|
|
|
|
const body = await req.json().catch(() => null);
|
|
const currentPassword = typeof body?.currentPassword === "string" ? body.currentPassword : "";
|
|
const newPassword = typeof body?.newPassword === "string" ? body.newPassword : "";
|
|
|
|
const user = await prisma.user.findUniqueOrThrow({ where: { id: auth.user.id } });
|
|
|
|
if (!(await verifyPassword(currentPassword, user.passwordHash))) {
|
|
return NextResponse.json({ error: "รหัสผ่านปัจจุบันไม่ถูกต้อง" }, { status: 400 });
|
|
}
|
|
|
|
const problem = passwordProblem(newPassword);
|
|
if (problem) return NextResponse.json({ error: problem }, { status: 400 });
|
|
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: { passwordHash: await hashPassword(newPassword), mustChangePassword: false },
|
|
});
|
|
|
|
const token = await createSessionToken({
|
|
userId: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
role: user.globalRole,
|
|
mustChangePassword: false,
|
|
});
|
|
|
|
const res = NextResponse.json({ ok: true });
|
|
res.cookies.set(COOKIE_NAME, token, {
|
|
httpOnly: true,
|
|
secure: process.env.NODE_ENV === "production",
|
|
sameSite: "strict",
|
|
path: "/",
|
|
maxAge: SESSION_DURATION_SECONDS,
|
|
});
|
|
return res;
|
|
}
|