feat: set api auth

This commit is contained in:
hamster1963 2024-10-20 16:17:33 +08:00
parent 62e0adb97a
commit 20ed4a819f
9 changed files with 36 additions and 23 deletions

View File

@ -10,9 +10,8 @@ import { useLocale } from "next-intl";
import Image from "next/image"; import Image from "next/image";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { auth } from "@/auth";
async function Header() { function Header() {
const t = useTranslations("Header"); const t = useTranslations("Header");
const customLogo = getEnv("NEXT_PUBLIC_CustomLogo"); const customLogo = getEnv("NEXT_PUBLIC_CustomLogo");
const customTitle = getEnv("NEXT_PUBLIC_CustomTitle"); const customTitle = getEnv("NEXT_PUBLIC_CustomTitle");
@ -21,11 +20,6 @@ async function Header() {
const router = useRouter(); const router = useRouter();
const locale = useLocale(); const locale = useLocale();
const session = await auth();
if (session) {
console.log(session);
}
return ( return (
<div className="mx-auto w-full max-w-5xl"> <div className="mx-auto w-full max-w-5xl">

View File

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

View File

@ -1,21 +1,14 @@
import { auth } from "@/auth";
import ServerList from "@/components/ServerList"; import ServerList from "@/components/ServerList";
import ServerOverview from "@/components/ServerOverview"; import ServerOverview from "@/components/ServerOverview";
import { SignIn } from "@/components/sign-in";
import { unstable_setRequestLocale } from "next-intl/server"; import { unstable_setRequestLocale } from "next-intl/server";
export default async function Home({ export default function Home({
params: { locale }, params: { locale },
}: { }: {
params: { locale: string }; params: { locale: string };
}) { }) {
unstable_setRequestLocale(locale); unstable_setRequestLocale(locale);
const session = await auth()
if (!session) return <SignIn />
return ( return (
<div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6"> <div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6">
<ServerOverview /> <ServerOverview />

View File

@ -12,8 +12,9 @@ interface NezhaDataResponse {
export const GET = auth(async function GET(req) { export const GET = auth(async function GET(req) {
if (!req.auth) if (!req.auth) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 }); return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
}
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");

View File

@ -1,4 +1,5 @@
import { ServerMonitorChart } from "@/app/[locale]/types/nezha-api"; import { ServerMonitorChart } from "@/app/[locale]/types/nezha-api";
import { auth } from "@/auth";
import { GetServerMonitor } from "@/lib/serverFetch"; import { GetServerMonitor } from "@/lib/serverFetch";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
@ -9,7 +10,13 @@ interface NezhaDataResponse {
data?: ServerMonitorChart; data?: ServerMonitorChart;
} }
export async function GET(req: Request) { export const GET = auth(async function GET(req) {
if (!req.auth) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
}
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) {
@ -26,4 +33,4 @@ export async function GET(req: Request) {
return NextResponse.json({ error: response.error }, { status: 400 }); return NextResponse.json({ error: response.error }, { status: 400 });
} }
return NextResponse.json(response, { status: 200 }); return NextResponse.json(response, { status: 200 });
} });

View File

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

View File

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

BIN
bun.lockb

Binary file not shown.

View File

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