Merge branch 'main' into cloudflare

This commit is contained in:
hamster1963 2024-11-06 16:15:10 +08:00
commit 57e3f1b1bd
6 changed files with 36 additions and 20 deletions

View File

@ -8,16 +8,24 @@ import React from "react";
type DashboardProps = { type DashboardProps = {
children: React.ReactNode; children: React.ReactNode;
}; };
export default async function MainLayout({ children }: DashboardProps) { export default function MainLayout({ children }: DashboardProps) {
const session = await auth();
return ( return (
<div className="flex min-h-screen w-full flex-col"> <div className="flex min-h-screen w-full flex-col">
<main className="flex min-h-[calc(100vh_-_theme(spacing.16))] flex-1 flex-col gap-4 bg-background p-4 md:p-10 md:pt-8"> <main className="flex min-h-[calc(100vh_-_theme(spacing.16))] flex-1 flex-col gap-4 bg-background p-4 md:p-10 md:pt-8">
<Header /> <Header />
{!session && getEnv("SitePassword") ? <SignIn /> : children} <AuthProtected>{children}</AuthProtected>
<Footer /> <Footer />
</main> </main>
</div> </div>
); );
} }
async function AuthProtected({ children }: DashboardProps) {
if (getEnv("SitePassword")) {
const session = await auth();
if (!session) {
return <SignIn />;
}
}
return children;
}

View File

@ -14,10 +14,11 @@ interface ResError extends Error {
} }
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
const session = await auth(); if (getEnv("SitePassword")) {
const session = await auth();
if (!session && getEnv("SitePassword")) { if (!session) {
redirect("/"); redirect("/");
}
} }
const { searchParams } = new URL(req.url); const { searchParams } = new URL(req.url);

View File

@ -14,14 +14,16 @@ interface ResError extends Error {
} }
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
const session = await auth(); if (getEnv("SitePassword")) {
const session = await auth();
if (!session && getEnv("SitePassword")) { if (!session) {
redirect("/"); redirect("/");
}
} }
const { searchParams } = new URL(req.url); const { searchParams } = new URL(req.url);
const server_id = searchParams.get("server_id"); const server_id = searchParams.get("server_id");
if (!server_id) { if (!server_id) {
return NextResponse.json( return NextResponse.json(
{ error: "server_id is required" }, { error: "server_id is required" },

View File

@ -14,10 +14,11 @@ interface ResError extends Error {
} }
export async function GET() { export async function GET() {
const session = await auth(); if (getEnv("SitePassword")) {
const session = await auth();
if (!session && getEnv("SitePassword")) { if (!session) {
redirect("/"); redirect("/");
}
} }
try { try {

View File

@ -48,7 +48,7 @@ export default async function LocaleLayout({
return ( return (
<html lang={locale} suppressHydrationWarning> <html lang={locale} suppressHydrationWarning>
<head> <head>
<PublicEnvScript /> {!process.env.VERCEL && !process.env.CF_PAGES && <PublicEnvScript />}
<link <link
rel="stylesheet" rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@7.0.0/css/flag-icons.min.css" href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@7.0.0/css/flag-icons.min.css"

View File

@ -1,8 +1,12 @@
import { env } from "next-runtime-env"; import { env } from "next-runtime-env";
export default function getEnv(key: string) { export default function getEnv(key: string) {
if (key.startsWith("NEXT_PUBLIC_")) { if (process.env.VERCEL || process.env.CF_PAGES) {
return env(key); return process.env[key];
} else {
if (key.startsWith("NEXT_PUBLIC_")) {
return env(key);
}
return process.env[key];
} }
return process.env[key];
} }