feat: add api auth redirect

This commit is contained in:
hamster1963 2024-10-21 10:25:00 +08:00
parent f2b47d5cbf
commit f42cc026ac
4 changed files with 6 additions and 4 deletions

View File

@ -3,7 +3,6 @@ import Header from "@/app/[locale]/(main)/header";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { SignIn } from "@/components/SignIn"; import { SignIn } from "@/components/SignIn";
import getEnv from "@/lib/env-entry"; import getEnv from "@/lib/env-entry";
import { redirect } from "next/navigation";
import React from "react"; import React from "react";
type DashboardProps = { type DashboardProps = {

View File

@ -2,6 +2,7 @@ import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api";
import { auth } from "@/auth"; import { auth } from "@/auth";
import getEnv from "@/lib/env-entry"; import getEnv from "@/lib/env-entry";
import { GetServerDetail } from "@/lib/serverFetch"; import { GetServerDetail } from "@/lib/serverFetch";
import { redirect } from "next/navigation";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
@ -13,7 +14,7 @@ interface NezhaDataResponse {
export const GET = auth(async function GET(req) { export const GET = auth(async function GET(req) {
if (!req.auth && getEnv("SitePassword")) { if (!req.auth && getEnv("SitePassword")) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 }); redirect("/api/auth/signin");
} }
const { searchParams } = new URL(req.url); const { searchParams } = new URL(req.url);

View File

@ -2,6 +2,7 @@ import { ServerMonitorChart } from "@/app/[locale]/types/nezha-api";
import { auth } from "@/auth"; import { auth } from "@/auth";
import getEnv from "@/lib/env-entry"; import getEnv from "@/lib/env-entry";
import { GetServerMonitor } from "@/lib/serverFetch"; import { GetServerMonitor } from "@/lib/serverFetch";
import { redirect } from "next/navigation";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
@ -13,7 +14,7 @@ interface NezhaDataResponse {
export const GET = auth(async function GET(req) { export const GET = auth(async function GET(req) {
if (!req.auth && getEnv("SitePassword")) { if (!req.auth && getEnv("SitePassword")) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 }); redirect("/api/auth/signin");
} }
const { searchParams } = new URL(req.url); const { searchParams } = new URL(req.url);

View File

@ -2,6 +2,7 @@ import { ServerApi } from "@/app/[locale]/types/nezha-api";
import { auth } from "@/auth"; import { auth } from "@/auth";
import getEnv from "@/lib/env-entry"; import getEnv from "@/lib/env-entry";
import { GetNezhaData } from "@/lib/serverFetch"; import { GetNezhaData } from "@/lib/serverFetch";
import { redirect } from "next/navigation";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
@ -13,7 +14,7 @@ interface NezhaDataResponse {
export const GET = auth(async function GET(req) { export const GET = auth(async function GET(req) {
if (!req.auth && getEnv("SitePassword")) { if (!req.auth && getEnv("SitePassword")) {
return NextResponse.json({ message: "Not authenticated" }, { status: 401 }); redirect("/api/auth/signin");
} }
const response = (await GetNezhaData()) as NezhaDataResponse; const response = (await GetNezhaData()) as NezhaDataResponse;