mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
feat: set api auth
This commit is contained in:
parent
62e0adb97a
commit
20ed4a819f
@ -10,9 +10,8 @@ import { useLocale } from "next-intl";
|
||||
import Image from "next/image";
|
||||
import { useRouter } from "next/navigation";
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import { auth } from "@/auth";
|
||||
|
||||
async function Header() {
|
||||
function Header() {
|
||||
const t = useTranslations("Header");
|
||||
const customLogo = getEnv("NEXT_PUBLIC_CustomLogo");
|
||||
const customTitle = getEnv("NEXT_PUBLIC_CustomTitle");
|
||||
@ -21,11 +20,6 @@ async function Header() {
|
||||
const router = useRouter();
|
||||
const locale = useLocale();
|
||||
|
||||
const session = await auth();
|
||||
|
||||
if (session) {
|
||||
console.log(session);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mx-auto w-full max-w-5xl">
|
||||
|
@ -1,11 +1,16 @@
|
||||
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 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) 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">
|
||||
|
@ -1,21 +1,14 @@
|
||||
import { auth } from "@/auth";
|
||||
import ServerList from "@/components/ServerList";
|
||||
import ServerOverview from "@/components/ServerOverview";
|
||||
import { SignIn } from "@/components/sign-in";
|
||||
import { unstable_setRequestLocale } from "next-intl/server";
|
||||
|
||||
export default async function Home({
|
||||
export default function Home({
|
||||
params: { locale },
|
||||
}: {
|
||||
params: { locale: string };
|
||||
}) {
|
||||
unstable_setRequestLocale(locale);
|
||||
|
||||
const session = await auth()
|
||||
if (!session) return <SignIn />
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6">
|
||||
<ServerOverview />
|
||||
|
@ -12,8 +12,9 @@ interface NezhaDataResponse {
|
||||
|
||||
export const GET = auth(async function GET(req) {
|
||||
|
||||
if (!req.auth)
|
||||
if (!req.auth) {
|
||||
return NextResponse.json({ message: "Not authenticated" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(req.url);
|
||||
const server_id = searchParams.get("server_id");
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { ServerMonitorChart } from "@/app/[locale]/types/nezha-api";
|
||||
import { auth } from "@/auth";
|
||||
import { GetServerMonitor } from "@/lib/serverFetch";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
@ -9,7 +10,13 @@ interface NezhaDataResponse {
|
||||
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 server_id = searchParams.get("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(response, { status: 200 });
|
||||
}
|
||||
});
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { ServerApi } from "@/app/[locale]/types/nezha-api";
|
||||
import { auth } from "@/auth";
|
||||
import { GetNezhaData } from "@/lib/serverFetch";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
@ -9,11 +10,16 @@ interface NezhaDataResponse {
|
||||
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;
|
||||
if (response.error) {
|
||||
console.log(response.error);
|
||||
return NextResponse.json({ error: response.error }, { status: 400 });
|
||||
}
|
||||
return NextResponse.json(response, { status: 200 });
|
||||
}
|
||||
});
|
||||
|
6
auth.ts
6
auth.ts
@ -1,15 +1,17 @@
|
||||
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 === "123456") {
|
||||
return { id: "1", name: "John Doe" }
|
||||
if (credentials.password === getEnv("SITE_PASSWORD")) {
|
||||
return { id: "0" }
|
||||
}
|
||||
return null
|
||||
},
|
||||
|
@ -23,6 +23,11 @@ const withPWA = withPWAInit({
|
||||
const nextConfig = {
|
||||
output: "standalone",
|
||||
reactStrictMode: true,
|
||||
experimental: {
|
||||
serverActions: {
|
||||
allowedOrigins: ['*'],
|
||||
},
|
||||
},
|
||||
logging: {
|
||||
fetches: {
|
||||
fullUrl: true,
|
||||
|
Loading…
Reference in New Issue
Block a user