mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
feat: init auth
This commit is contained in:
parent
5dba593bf0
commit
62e0adb97a
@ -10,8 +10,9 @@ 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";
|
||||||
|
|
||||||
function Header() {
|
async 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");
|
||||||
@ -20,6 +21,12 @@ 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">
|
||||||
<section className="flex items-center justify-between">
|
<section className="flex items-center justify-between">
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
|
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 function Home({
|
export default async 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 />
|
||||||
|
@ -13,6 +13,7 @@ import { Inter as FontSans } from "next/font/google";
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import "/node_modules/flag-icons/css/flag-icons.min.css";
|
import "/node_modules/flag-icons/css/flag-icons.min.css";
|
||||||
|
import { auth } from "@/auth";
|
||||||
|
|
||||||
const fontSans = FontSans({
|
const fontSans = FontSans({
|
||||||
subsets: ["latin"],
|
subsets: ["latin"],
|
||||||
@ -54,6 +55,7 @@ export default function LocaleLayout({
|
|||||||
unstable_setRequestLocale(locale);
|
unstable_setRequestLocale(locale);
|
||||||
|
|
||||||
const messages = useMessages();
|
const messages = useMessages();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<html lang={locale} suppressHydrationWarning>
|
<html lang={locale} suppressHydrationWarning>
|
||||||
<head>
|
<head>
|
||||||
|
2
app/api/auth/[...nextauth]/route.ts
Normal file
2
app/api/auth/[...nextauth]/route.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
import { handlers } from "@/auth" // Referring to the auth.ts we just created
|
||||||
|
export const { GET, POST } = handlers
|
@ -1,6 +1,7 @@
|
|||||||
import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api";
|
import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api";
|
||||||
import { GetServerDetail } from "@/lib/serverFetch";
|
import { GetServerDetail } from "@/lib/serverFetch";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { auth } from "@/auth"
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
@ -9,7 +10,11 @@ interface NezhaDataResponse {
|
|||||||
data?: NezhaAPISafe;
|
data?: NezhaAPISafe;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 +31,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 });
|
||||||
}
|
});
|
||||||
|
18
auth.ts
Normal file
18
auth.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import NextAuth from "next-auth"
|
||||||
|
import Credentials from "next-auth/providers/credentials"
|
||||||
|
|
||||||
|
export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||||
|
providers: [
|
||||||
|
Credentials({
|
||||||
|
credentials: {
|
||||||
|
password: {},
|
||||||
|
},
|
||||||
|
authorize: async (credentials) => {
|
||||||
|
if (credentials.password === "123456") {
|
||||||
|
return { id: "1", name: "John Doe" }
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
18
components/sign-in.tsx
Normal file
18
components/sign-in.tsx
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { signIn } from "@/auth"
|
||||||
|
|
||||||
|
export function SignIn() {
|
||||||
|
return (
|
||||||
|
<form
|
||||||
|
action={async (formData) => {
|
||||||
|
"use server"
|
||||||
|
await signIn("credentials", formData)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<label>
|
||||||
|
Password
|
||||||
|
<input name="password" type="password" />
|
||||||
|
</label>
|
||||||
|
<button>Sign In</button>
|
||||||
|
</form>
|
||||||
|
)
|
||||||
|
}
|
@ -3,6 +3,8 @@ import createMiddleware from "next-intl/middleware";
|
|||||||
|
|
||||||
import { defaultLocale, locales } from "./i18n-metadata";
|
import { defaultLocale, locales } from "./i18n-metadata";
|
||||||
|
|
||||||
|
// export { auth as middleware } from "@/auth"
|
||||||
|
|
||||||
export default createMiddleware({
|
export default createMiddleware({
|
||||||
// A list of all locales that are supported
|
// A list of all locales that are supported
|
||||||
locales: locales,
|
locales: locales,
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
"lucide-react": "^0.451.0",
|
"lucide-react": "^0.451.0",
|
||||||
"luxon": "^3.5.0",
|
"luxon": "^3.5.0",
|
||||||
"next": "^14.2.15",
|
"next": "^14.2.15",
|
||||||
|
"next-auth": "^5.0.0-beta.25",
|
||||||
"next-intl": "^3.21.1",
|
"next-intl": "^3.21.1",
|
||||||
"next-runtime-env": "^3.2.2",
|
"next-runtime-env": "^3.2.2",
|
||||||
"next-themes": "^0.3.0",
|
"next-themes": "^0.3.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user