From ca0f12774be3083e596a8ff8c2c465d205175432 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Mon, 21 Oct 2024 11:28:44 +0800 Subject: [PATCH 1/3] fix: docker auth_url --- app/api/auth/[...nextauth]/route.ts | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 4ddc1da..86c9f3d 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -1,22 +1,3 @@ import { handlers } from "@/auth"; -import { NextRequest } from "next/server"; -const reqWithTrustedOrigin = (req: NextRequest): NextRequest => { - const proto = req.headers.get("x-forwarded-proto"); - const host = req.headers.get("x-forwarded-host"); - if (!proto || !host) { - console.warn("Missing x-forwarded-proto or x-forwarded-host headers."); - return req; - } - const envOrigin = `${proto}://${host}`; - const { href, origin } = req.nextUrl; - return new NextRequest(href.replace(origin, envOrigin), req); -}; - -export const GET = (req: NextRequest) => { - return handlers.GET(reqWithTrustedOrigin(req)); -}; - -export const POST = (req: NextRequest) => { - return handlers.POST(reqWithTrustedOrigin(req)); -}; +export const { GET, POST } = handlers; From b7085f3d41b0838112a676ad6403290529100a56 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Mon, 21 Oct 2024 11:39:10 +0800 Subject: [PATCH 2/3] feat: add auth env --- auth.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auth.ts b/auth.ts index dc90f57..71604fb 100644 --- a/auth.ts +++ b/auth.ts @@ -4,8 +4,8 @@ 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", - trustHost: true, + secret: process.env.AUTH_SECRET ?? "this_is_nezha_dash_web_secret", + trustHost: process.env.AUTH_TRUST_HOST as boolean | undefined ?? true, pages: { signIn: "/", }, From 667ae590bb15e2b200aacf3e5d44081b79da504e Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Mon, 21 Oct 2024 12:20:50 +0800 Subject: [PATCH 3/3] refactor: signin logic --- components/SignIn.tsx | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/components/SignIn.tsx b/components/SignIn.tsx index 1bcca2d..e17507d 100644 --- a/components/SignIn.tsx +++ b/components/SignIn.tsx @@ -1,8 +1,8 @@ "use client"; -import { getCsrfToken } from "next-auth/react"; +import { getCsrfToken, signIn } from "next-auth/react"; import { useTranslations } from "next-intl"; -import { useSearchParams } from "next/navigation"; +import { useSearchParams, useRouter } from "next/navigation"; import { useEffect, useState } from "react"; export function SignIn() { @@ -13,6 +13,7 @@ export function SignIn() { const search = useSearchParams(); const error = search.get("error"); + const router = useRouter(); useEffect(() => { if (error) { @@ -28,11 +29,26 @@ export function SignIn() { loadProviders(); }, []); + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + const formData = new FormData(e.currentTarget); + const password = formData.get("password"); + const res = await signIn("credentials", { + password, + redirect: false, + }); + if (res?.error) { + setErrorState(true); + } else { + router.push("/"); + router.refresh(); + } + }; + return (