mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
Merge pull request #80 from hamster1963/fix-docker
refactor: sign-in login
This commit is contained in:
commit
17a9b20cda
@ -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;
|
||||
|
4
auth.ts
4
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: "/",
|
||||
},
|
||||
|
@ -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">
|
||||
|
Loading…
Reference in New Issue
Block a user