feat: init auth

This commit is contained in:
hamster1963 2024-10-20 15:42:05 +08:00
parent 0c4a7be53e
commit 7035e8d94c
10 changed files with 67 additions and 4 deletions

View File

@ -11,8 +11,9 @@ 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";
function Header() {
async function Header() {
const t = useTranslations("Header");
const customLogo = getEnv("NEXT_PUBLIC_CustomLogo");
const customTitle = getEnv("NEXT_PUBLIC_CustomTitle");
@ -21,6 +22,12 @@ 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">
<section className="flex items-center justify-between">

View File

@ -1,13 +1,21 @@
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 function Home({
export default async 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 />

View File

@ -13,6 +13,7 @@ import { Inter as FontSans } from "next/font/google";
import React from "react";
import "/node_modules/flag-icons/css/flag-icons.min.css";
import { auth } from "@/auth";
const fontSans = FontSans({
subsets: ["latin"],
@ -59,6 +60,7 @@ export default function LocaleLayout({
unstable_setRequestLocale(locale);
const messages = useMessages();
return (
<html lang={locale} suppressHydrationWarning>
<head>

View File

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

View File

@ -1,6 +1,7 @@
import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api";
import { GetServerDetail } from "@/lib/serverFetch";
import { NextResponse } from "next/server";
import { auth } from "@/auth"
export const runtime = 'edge';
@ -11,7 +12,11 @@ interface NezhaDataResponse {
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 server_id = searchParams.get("server_id");
if (!server_id) {
@ -28,4 +33,4 @@ export async function GET(req: Request) {
return NextResponse.json({ error: response.error }, { status: 400 });
}
return NextResponse.json(response, { status: 200 });
}
});

18
auth.ts Normal file
View 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
},
}),
],
})

BIN
bun.lockb

Binary file not shown.

18
components/sign-in.tsx Normal file
View 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>
)
}

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

@ -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",