feat(auth): Enhance authentication flow and settings management
This commit is contained in:
12
src/app/api/settings/require-login/route.js
Normal file
12
src/app/api/settings/require-login/route.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getSettings } from "@/lib/localDb";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const settings = await getSettings();
|
||||
const requireLogin = settings.requireLogin !== false;
|
||||
return NextResponse.json({ requireLogin });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ requireLogin: true }, { status: 200 });
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,15 @@ import bcrypt from "bcryptjs";
|
||||
export async function GET() {
|
||||
try {
|
||||
const settings = await getSettings();
|
||||
// Don't return the password hash to the client
|
||||
const { password, ...safeSettings } = settings;
|
||||
|
||||
// Add ENABLE_REQUEST_LOGS from env
|
||||
const enableRequestLogs = process.env.ENABLE_REQUEST_LOGS === "true";
|
||||
|
||||
return NextResponse.json({ ...safeSettings, enableRequestLogs });
|
||||
return NextResponse.json({
|
||||
...safeSettings,
|
||||
enableRequestLogs,
|
||||
hasPassword: !!password
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error getting settings:", error);
|
||||
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||
@@ -37,8 +39,9 @@ export async function PATCH(request) {
|
||||
return NextResponse.json({ error: "Invalid current password" }, { status: 401 });
|
||||
}
|
||||
} else {
|
||||
// First time setting password, check if it matches default 123456
|
||||
if (body.currentPassword !== "123456") {
|
||||
// First time setting password, no current password needed
|
||||
// Allow empty currentPassword or default "123456"
|
||||
if (body.currentPassword && body.currentPassword !== "123456") {
|
||||
return NextResponse.json({ error: "Invalid current password" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user