Merge branch 'main' into cloudflare

This commit is contained in:
hamster1963 2024-10-21 12:23:54 +08:00
commit 9c3225d302
3 changed files with 23 additions and 28 deletions

View File

@ -1,25 +1,4 @@
import { handlers } from "@/auth";
import { NextRequest } from "next/server";
export const runtime = "edge";
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;

View File

@ -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: "/",
},

View File

@ -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<HTMLFormElement>) => {
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 (
<form
className="flex flex-col items-center justify-start gap-4 p-4 "
method="post"
action="/api/auth/callback/credentials"
onSubmit={handleSubmit}
>
<input type="hidden" name="csrfToken" value={csrfToken} />
<section className="flex flex-col items-start gap-2">