Merge pull request #78 from hamster1963/auth

feat: add site password
This commit is contained in:
仓鼠 2024-10-21 02:24:55 +08:00 committed by GitHub
commit a70a6a5645
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 120 additions and 7 deletions

View File

@ -1,11 +1,25 @@
import Footer from "@/app/[locale]/(main)/footer";
import Header from "@/app/[locale]/(main)/header";
import { auth } from "@/auth";
import { SignIn } from "@/components/sign-in";
import getEnv from "@/lib/env-entry";
import { redirect } from "next/navigation";
import React from "react";
type DashboardProps = {
children: React.ReactNode;
};
export default function MainLayout({ children }: DashboardProps) {
export default async function MainLayout({ children }: DashboardProps) {
const session = await auth();
if (!session && getEnv("SITE_PASSWORD")) {
if (getEnv("CF_PAGES")) {
redirect("/api/auth/signin");
} else {
return <SignIn />;
}
}
return (
<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-muted/40 p-4 md:p-10 md:pt-8">

View File

@ -8,6 +8,7 @@ export default function Home({
params: { locale: string };
}) {
unstable_setRequestLocale(locale);
return (
<div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6">
<ServerOverview />

View File

@ -1,4 +1,5 @@
// @auto-i18n-check. Please do not delete the line.
import { auth } from "@/auth";
import { locales } from "@/i18n-metadata";
import getEnv from "@/lib/env-entry";
import { cn } from "@/lib/utils";
@ -54,6 +55,7 @@ export default function LocaleLayout({
unstable_setRequestLocale(locale);
const messages = useMessages();
return (
<html lang={locale} suppressHydrationWarning>
<head>

View File

@ -0,0 +1,4 @@
import { handlers } from "@/auth";
// Referring to the auth.ts we just created
export const { GET, POST } = handlers;

View File

@ -1,4 +1,6 @@
import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api";
import { auth } from "@/auth";
import getEnv from "@/lib/env-entry";
import { GetServerDetail } from "@/lib/serverFetch";
import { NextResponse } from "next/server";
@ -9,7 +11,11 @@ interface NezhaDataResponse {
data?: NezhaAPISafe;
}
export async function GET(req: Request) {
export const GET = auth(async function GET(req) {
if (!req.auth && getEnv("SITE_PASSWORD")) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
}
const { searchParams } = new URL(req.url);
const server_id = searchParams.get("server_id");
if (!server_id) {
@ -26,4 +32,4 @@ export async function GET(req: Request) {
return NextResponse.json({ error: response.error }, { status: 400 });
}
return NextResponse.json(response, { status: 200 });
}
});

View File

@ -1,4 +1,6 @@
import { ServerMonitorChart } from "@/app/[locale]/types/nezha-api";
import { auth } from "@/auth";
import getEnv from "@/lib/env-entry";
import { GetServerMonitor } from "@/lib/serverFetch";
import { NextResponse } from "next/server";
@ -9,7 +11,11 @@ interface NezhaDataResponse {
data?: ServerMonitorChart;
}
export async function GET(req: Request) {
export const GET = auth(async function GET(req) {
if (!req.auth && getEnv("SITE_PASSWORD")) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
}
const { searchParams } = new URL(req.url);
const server_id = searchParams.get("server_id");
if (!server_id) {
@ -26,4 +32,4 @@ export async function GET(req: Request) {
return NextResponse.json({ error: response.error }, { status: 400 });
}
return NextResponse.json(response, { status: 200 });
}
});

View File

@ -1,4 +1,6 @@
import { ServerApi } from "@/app/[locale]/types/nezha-api";
import { auth } from "@/auth";
import getEnv from "@/lib/env-entry";
import { GetNezhaData } from "@/lib/serverFetch";
import { NextResponse } from "next/server";
@ -9,11 +11,15 @@ interface NezhaDataResponse {
data?: ServerApi;
}
export async function GET(_: Request) {
export const GET = auth(async function GET(req) {
if (!req.auth && getEnv("SITE_PASSWORD")) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
}
const response = (await GetNezhaData()) as NezhaDataResponse;
if (response.error) {
console.log(response.error);
return NextResponse.json({ error: response.error }, { status: 400 });
}
return NextResponse.json(response, { status: 200 });
}
});

21
auth.ts Normal file
View File

@ -0,0 +1,21 @@
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import getEnv from "./lib/env-entry";
export const { handlers, signIn, signOut, auth } = NextAuth({
secret: "this_is_nezha_dash_web_secret",
providers: [
Credentials({
credentials: {
password: {},
},
authorize: async (credentials) => {
if (credentials.password === getEnv("SITE_PASSWORD")) {
return { id: "0" };
}
return null;
},
}),
],
});

BIN
bun.lockb

Binary file not shown.

45
components/sign-in.tsx Normal file
View File

@ -0,0 +1,45 @@
import Footer from "@/app/[locale]/(main)/footer";
import Header from "@/app/[locale]/(main)/header";
import { signIn } from "@/auth";
import { useLocale } from "next-intl";
import { redirect } from "next/navigation";
export function SignIn() {
const locale = useLocale();
async function handleSubmit(formData: FormData) {
"use server";
try {
await signIn("credentials", formData);
} catch (error) {
redirect(`/${locale}`);
}
}
return (
<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-muted/40 p-4 md:p-10 md:pt-8">
<Header />
<form
className="flex flex-col items-center justify-start gap-4 p-4 "
action={handleSubmit}
>
<section className="flex flex-col items-start gap-2">
<label className="flex flex-col items-start gap-1 ">
<p className="text-base font-semibold"></p>
<input
className="px-1 border-[1px] rounded-[5px]"
name="password"
type="password"
/>
</label>
<button className=" px-1.5 py-0.5 w-fit text-sm font-semibold rounded-[8px] border bg-card hover:brightness-95 transition-all text-card-foreground shadow-lg shadow-neutral-200/40 dark:shadow-none">
</button>
</section>
</form>
<Footer />
</main>
</div>
);
}

View File

@ -3,6 +3,8 @@ import createMiddleware from "next-intl/middleware";
import { defaultLocale, locales } from "./i18n-metadata";
// export { auth as middleware } from "@/auth"
export default createMiddleware({
// A list of all locales that are supported
locales: locales,

View File

@ -23,6 +23,11 @@ const withPWA = withPWAInit({
const nextConfig = {
output: "standalone",
reactStrictMode: true,
experimental: {
serverActions: {
allowedOrigins: ["*"],
},
},
logging: {
fetches: {
fullUrl: true,

View File

@ -33,6 +33,7 @@
"lucide-react": "^0.451.0",
"luxon": "^3.5.0",
"next": "^14.2.15",
"next-auth": "^5.0.0-beta.25",
"next-intl": "^3.21.1",
"next-runtime-env": "^3.2.2",
"next-themes": "^0.3.0",