feat: add first-run admin setup
This commit is contained in:
1 parent
dd2fc71867
commit
2aaa9335e3
16 files changed
+644
-152
No files matched your search
+11
-2
@@ -3,8 +3,17 @@ import { Features } from '@/components/sections/Features';
|
||||
import { Hero } from '@/components/sections/Hero';
|
||||
import { LandingAbout } from '@/components/sections/LandingAbout';
|
||||
import { SdkSection } from '@/components/sections/SdkSection';
|
||||
import { isAdminSetupComplete } from '@/core/auth/adminUsers';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export default async function LandingPage() {
|
||||
if (process.env.NODE_ENV === 'production' && process.env.DATABASE_URL) {
|
||||
const isConfigured = await isAdminSetupComplete().catch(() => false);
|
||||
if (!isConfigured) redirect('/vcon/setup');
|
||||
}
|
||||
|
||||
export default function LandingPage() {
|
||||
return (
|
||||
<MainLayout>
|
||||
<Hero />
|
||||
@@ -13,4 +22,4 @@ export default function LandingPage() {
|
||||
<LandingAbout />
|
||||
</MainLayout>
|
||||
);
|
||||
}
|
||||
}
|
||||
+31
-14
@@ -1,4 +1,5 @@
|
||||
import { createAdminSessionToken } from "@/core/auth/adminSession";
|
||||
import { verifyAdminCredentials } from "@/core/auth/adminUsers";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { createHash, timingSafeEqual } from "node:crypto";
|
||||
|
||||
@@ -13,20 +14,12 @@ function constantTimeEqual(left: string, right: string) {
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const adminEmail = process.env.DATASTACK_ADMIN_EMAIL;
|
||||
const adminPassword = process.env.DATASTACK_ADMIN_PASSWORD;
|
||||
const sessionSecret = process.env.DATASTACK_ADMIN_SESSION_SECRET;
|
||||
|
||||
if (
|
||||
!adminEmail ||
|
||||
!adminPassword ||
|
||||
!sessionSecret ||
|
||||
sessionSecret.length < 32
|
||||
) {
|
||||
if (!sessionSecret || sessionSecret.length < 32) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
"Admin login is not configured. Set the required server environment variables.",
|
||||
error: "Admin authentication is not configured.",
|
||||
},
|
||||
{ status: 503 },
|
||||
);
|
||||
@@ -37,11 +30,35 @@ export async function POST(request: NextRequest) {
|
||||
const email =
|
||||
typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
|
||||
const password = typeof body.password === "string" ? body.password : "";
|
||||
if (!email || password.length === 0 || password.length > 1024) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid email or password." },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
!constantTimeEqual(email, adminEmail.trim().toLowerCase()) ||
|
||||
!constantTimeEqual(password, adminPassword)
|
||||
) {
|
||||
let authenticated = false;
|
||||
if (process.env.NODE_ENV !== "production" && !process.env.DATABASE_URL) {
|
||||
const adminEmail = process.env.DATASTACK_ADMIN_EMAIL;
|
||||
const adminPassword = process.env.DATASTACK_ADMIN_PASSWORD;
|
||||
authenticated = Boolean(
|
||||
adminEmail &&
|
||||
adminPassword &&
|
||||
constantTimeEqual(email, adminEmail.trim().toLowerCase()) &&
|
||||
constantTimeEqual(password, adminPassword),
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
authenticated = await verifyAdminCredentials(email, password);
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin authentication is temporarily unavailable." },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!authenticated) {
|
||||
return NextResponse.json(
|
||||
{ error: "Invalid email or password." },
|
||||
{ status: 401 },
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import { createAdminSessionToken } from "@/core/auth/adminSession";
|
||||
import {
|
||||
createInitialAdmin,
|
||||
isAdminSetupComplete,
|
||||
verifyAdminSetupKey,
|
||||
} from "@/core/auth/adminUsers";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const SESSION_COOKIE = "datastack_admin_session";
|
||||
const SESSION_SECONDS = 8 * 60 * 60;
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
function validEmail(email: string) {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const sessionSecret = process.env.DATASTACK_ADMIN_SESSION_SECRET;
|
||||
const configuredAdminEmail =
|
||||
process.env.DATASTACK_ADMIN_EMAIL?.trim().toLowerCase();
|
||||
if (!sessionSecret || sessionSecret.length < 32) {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin session configuration is unavailable." },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
if (!configuredAdminEmail || !validEmail(configuredAdminEmail)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin setup configuration is unavailable." },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
const email =
|
||||
typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
|
||||
const password = typeof body.password === "string" ? body.password : "";
|
||||
const setupKey = typeof body.setupKey === "string" ? body.setupKey : "";
|
||||
|
||||
if (!validEmail(email)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Enter a valid email address." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
if (email !== configuredAdminEmail) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Use the administrator email configured for this deployment.",
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
if (password.length < 12 || password.length > 256) {
|
||||
return NextResponse.json(
|
||||
{ error: "Choose a password between 12 and 256 characters." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
if (!verifyAdminSetupKey(setupKey)) {
|
||||
return NextResponse.json(
|
||||
{ error: "The installation key is invalid." },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
if (await isAdminSetupComplete()) {
|
||||
return NextResponse.json(
|
||||
{ error: "Initial admin setup has already been completed." },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
|
||||
const created = await createInitialAdmin(email, password);
|
||||
if (!created) {
|
||||
return NextResponse.json(
|
||||
{ error: "Initial admin setup has already been completed." },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
|
||||
const token = await createAdminSessionToken(
|
||||
email,
|
||||
sessionSecret,
|
||||
Date.now() + SESSION_SECONDS * 1000,
|
||||
);
|
||||
const response = NextResponse.json({ success: true });
|
||||
response.cookies.set(SESSION_COOKIE, token, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "strict",
|
||||
path: "/",
|
||||
maxAge: SESSION_SECONDS,
|
||||
});
|
||||
return response;
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin setup is temporarily unavailable." },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { isAdminSetupComplete } from "@/core/auth/adminUsers";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
return NextResponse.json(
|
||||
{ setupComplete: await isAdminSetupComplete() },
|
||||
{ headers: { "Cache-Control": "no-store" } },
|
||||
);
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: "Admin setup status is unavailable." },
|
||||
{ status: 503, headers: { "Cache-Control": "no-store" } },
|
||||
);
|
||||
}
|
||||
}
|
||||
+11
-1
@@ -1,5 +1,15 @@
|
||||
import { AdminLogin } from '@/components/sections/AdminLogin';
|
||||
import { isAdminSetupComplete } from '@/core/auth/adminUsers';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export default async function LoginPage() {
|
||||
if (process.env.NODE_ENV === 'production' && process.env.DATABASE_URL) {
|
||||
const isConfigured = await isAdminSetupComplete().catch(() => false);
|
||||
if (!isConfigured) redirect('/vcon/setup');
|
||||
}
|
||||
|
||||
export default function LoginPage() {
|
||||
return <AdminLogin />;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { AdminSetup } from '@/components/sections/AdminSetup';
|
||||
import { isAdminSetupComplete } from '@/core/auth/adminUsers';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export default async function SetupPage() {
|
||||
let databaseReady = true;
|
||||
try {
|
||||
if (await isAdminSetupComplete()) redirect('/vcon/login');
|
||||
} catch {
|
||||
databaseReady = false;
|
||||
}
|
||||
|
||||
return (
|
||||
<AdminSetup
|
||||
databaseReady={databaseReady}
|
||||
initialEmail={process.env.DATASTACK_ADMIN_EMAIL?.trim().toLowerCase() ?? ''}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user