release: 2026-08-01
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import NextAuth, { NextAuthOptions } from "next-auth";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { getDb } from "@/lib/db";
|
||||
import { isRateLimited } from "@/lib/rate-limit";
|
||||
|
||||
type UserRow = { id: string; email: string; password_hash: string };
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
name: "Credentials",
|
||||
credentials: {
|
||||
email: { label: "Email", type: "email" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
if (!credentials?.email || !credentials?.password) return null;
|
||||
|
||||
// Throttle login attempts per IP — this is the password brute-force surface.
|
||||
if (isRateLimited(req, "login", 10, 15 * 60 * 1000)) {
|
||||
throw new Error("Too many attempts. Try again later.");
|
||||
}
|
||||
|
||||
const db = getDb();
|
||||
const user = db
|
||||
.prepare("SELECT id, email, password_hash FROM users WHERE email = ?")
|
||||
.get(credentials.email) as UserRow | undefined;
|
||||
|
||||
if (!user) return null;
|
||||
|
||||
const valid = await bcrypt.compare(credentials.password, user.password_hash);
|
||||
if (!valid) return null;
|
||||
|
||||
return { id: user.id, email: user.email };
|
||||
},
|
||||
}),
|
||||
],
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
},
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
},
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
};
|
||||
|
||||
export default NextAuth(authOptions);
|
||||
@@ -0,0 +1,39 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "./[...nextauth]";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { getDb } from "@/lib/db";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method !== "POST") return res.status(405).end();
|
||||
|
||||
const session = await getServerSession(req, res, authOptions);
|
||||
if (!session?.user?.email) return res.status(401).json({ error: "Not authenticated" });
|
||||
|
||||
const { currentPassword, newPassword } = req.body as {
|
||||
currentPassword?: string;
|
||||
newPassword?: string;
|
||||
};
|
||||
|
||||
if (!currentPassword || !newPassword) {
|
||||
return res.status(400).json({ error: "Current and new password are required." });
|
||||
}
|
||||
if (newPassword.length < 8) {
|
||||
return res.status(400).json({ error: "New password must be at least 8 characters." });
|
||||
}
|
||||
|
||||
const db = getDb();
|
||||
const user = db
|
||||
.prepare("SELECT id, password_hash FROM users WHERE email = ?")
|
||||
.get(session.user.email) as { id: string; password_hash: string } | undefined;
|
||||
|
||||
if (!user) return res.status(404).json({ error: "User not found." });
|
||||
|
||||
const valid = await bcrypt.compare(currentPassword, user.password_hash);
|
||||
if (!valid) return res.status(400).json({ error: "Current password is incorrect." });
|
||||
|
||||
const hash = await bcrypt.hash(newPassword, 10);
|
||||
db.prepare("UPDATE users SET password_hash = ? WHERE id = ?").run(hash, user.id);
|
||||
|
||||
return res.status(200).json({ ok: true });
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { getDb } from "@/lib/db";
|
||||
import { randomUUID } from "crypto";
|
||||
import { isRateLimited } from "@/lib/rate-limit";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method !== "POST") return res.status(405).end();
|
||||
|
||||
// Invite code + password are both guessable secrets — throttle attempts per IP.
|
||||
if (isRateLimited(req, "signup", 10, 15 * 60 * 1000)) {
|
||||
return res.status(429).json({ error: "Too many attempts. Try again later." });
|
||||
}
|
||||
|
||||
const { email, password, inviteCode } = req.body as {
|
||||
email?: string;
|
||||
password?: string;
|
||||
inviteCode?: string;
|
||||
};
|
||||
|
||||
if (!email || !password || !inviteCode) {
|
||||
return res.status(400).json({ error: "Email, password, and invite code are required." });
|
||||
}
|
||||
|
||||
const authPassword = process.env.AUTH_PASSWORD;
|
||||
if (!authPassword) {
|
||||
return res.status(500).json({ error: "AUTH_PASSWORD is not configured on this server." });
|
||||
}
|
||||
|
||||
if (inviteCode !== authPassword) {
|
||||
return res.status(403).json({ error: "Invalid invite code." });
|
||||
}
|
||||
|
||||
if (password.length < 8) {
|
||||
return res.status(400).json({ error: "Password must be at least 8 characters." });
|
||||
}
|
||||
|
||||
const db = getDb();
|
||||
const existing = db.prepare("SELECT id FROM users WHERE email = ?").get(email);
|
||||
if (existing) {
|
||||
return res.status(409).json({ error: "An account with this email already exists." });
|
||||
}
|
||||
|
||||
const hash = await bcrypt.hash(password, 10);
|
||||
db.prepare("INSERT INTO users (id, email, password_hash) VALUES (?, ?, ?)").run(randomUUID(), email, hash);
|
||||
|
||||
return res.status(201).json({ ok: true });
|
||||
}
|
||||
Reference in New Issue
Block a user