mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-02-21 09:14:42 +08:00
@@ -146,7 +146,6 @@ Pull Bot 会反复触发无效的 PR 和垃圾邮件,严重干扰项目维护
|
||||
| NEXT_PUBLIC_ENABLE_BLOCKAD | 开启智能去广告功能(实验性) | true / false | false |
|
||||
| NEXT_PUBLIC_SEARCH_MAX_PAGE | 搜索接口可拉取的最大页数 | 1-50 | 5 |
|
||||
| NEXT_PUBLIC_AGGREGATE_SEARCH_RESULT | 搜索结果默认是否按标题和年份聚合 | true / false | true |
|
||||
| NEXT_PUBLIC_SITE_NAME | 站点名称 | 任意 string | MoonTV |
|
||||
|
||||
## 配置说明
|
||||
|
||||
@@ -205,10 +204,6 @@ MoonTV 支持标准的苹果 CMS V10 API 格式。
|
||||
- 如因公开分享导致的任何法律问题,用户需自行承担责任
|
||||
- 项目开发者不对用户的使用行为承担任何法律责任
|
||||
|
||||
## Star History
|
||||
|
||||
[](https://www.star-history.com/#senshinya/MoonTV&Date)
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) © 2025 MoonTV & Contributors
|
||||
|
||||
@@ -24,6 +24,41 @@ export async function POST(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// 登录成功:写入 HttpOnly Cookie
|
||||
const res = NextResponse.json({ ok: true });
|
||||
res.cookies.set({
|
||||
name: 'password',
|
||||
value: password,
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: 60 * 60 * 24 * 30, // 30 天
|
||||
path: '/',
|
||||
});
|
||||
|
||||
return res;
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// 使用 Cookie 校验登录状态
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const result = process.env.PASSWORD;
|
||||
|
||||
// 未设置 PASSWORD 则直接放行
|
||||
if (!result) {
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
const cookiePassword = req.cookies.get('password')?.value;
|
||||
const matched = cookiePassword === result;
|
||||
|
||||
if (!matched) {
|
||||
return NextResponse.json({ ok: false }, { status: 401 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: '服务器错误' }, { status: 500 });
|
||||
|
||||
@@ -3,22 +3,15 @@ import { Inter } from 'next/font/google';
|
||||
|
||||
import './globals.css';
|
||||
|
||||
import AuthProvider from '../components/AuthProvider';
|
||||
import { SiteNameProvider } from '../components/SiteNameContext';
|
||||
import { ThemeProvider } from '../components/ThemeProvider';
|
||||
|
||||
const inter = Inter({ subsets: ['latin'] });
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const runtime = 'edge';
|
||||
|
||||
export function generateMetadata(): Metadata {
|
||||
return {
|
||||
title: process.env.NEXT_PUBLIC_SITE_NAME || 'MoonTV',
|
||||
description: '影视聚合',
|
||||
manifest: '/manifest.json',
|
||||
};
|
||||
}
|
||||
export const metadata: Metadata = {
|
||||
title: 'MoonTV',
|
||||
description: '影视聚合',
|
||||
manifest: '/manifest.json',
|
||||
};
|
||||
|
||||
export const viewport: Viewport = {
|
||||
width: 'device-width',
|
||||
@@ -33,24 +26,19 @@ export default function RootLayout({
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const siteName = process.env.NEXT_PUBLIC_SITE_NAME || 'MoonTV';
|
||||
|
||||
return (
|
||||
<html lang='zh-CN' suppressHydrationWarning>
|
||||
<body
|
||||
className={`${inter.className} min-h-screen bg-white text-gray-900 dark:bg-black dark:text-gray-200`}
|
||||
data-site-name={siteName}
|
||||
>
|
||||
<SiteNameProvider value={siteName}>
|
||||
<ThemeProvider
|
||||
attribute='class'
|
||||
defaultTheme='system'
|
||||
enableSystem
|
||||
disableTransitionOnChange
|
||||
>
|
||||
<AuthProvider>{children}</AuthProvider>
|
||||
</ThemeProvider>
|
||||
</SiteNameProvider>
|
||||
<ThemeProvider
|
||||
attribute='class'
|
||||
defaultTheme='system'
|
||||
enableSystem
|
||||
disableTransitionOnChange
|
||||
>
|
||||
{children}
|
||||
</ThemeProvider>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
@@ -3,12 +3,9 @@
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import { Suspense, useState } from 'react';
|
||||
|
||||
import { useSiteName } from '@/components/SiteNameContext';
|
||||
import { ThemeToggle } from '@/components/ThemeToggle';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
function LoginPageClient({ siteName }: { siteName: string }) {
|
||||
function LoginPageClient() {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const [password, setPassword] = useState('');
|
||||
@@ -30,11 +27,6 @@ function LoginPageClient({ siteName }: { siteName: string }) {
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
// 保存密码以供后续请求使用
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('password', password);
|
||||
}
|
||||
|
||||
const redirect = searchParams.get('redirect') || '/';
|
||||
router.replace(redirect);
|
||||
} else if (res.status === 401) {
|
||||
@@ -55,7 +47,7 @@ function LoginPageClient({ siteName }: { siteName: string }) {
|
||||
</div>
|
||||
<div className='relative z-10 w-full max-w-md rounded-3xl bg-gradient-to-b from-white/90 via-white/70 to-white/40 dark:from-zinc-900/90 dark:via-zinc-900/70 dark:to-zinc-900/40 backdrop-blur-xl shadow-2xl p-10 dark:border dark:border-zinc-800'>
|
||||
<h1 className='text-green-600 tracking-tight text-center text-3xl font-extrabold mb-8 bg-clip-text drop-shadow-sm'>
|
||||
{siteName}
|
||||
MoonTV
|
||||
</h1>
|
||||
<form onSubmit={handleSubmit} className='space-y-8'>
|
||||
<div>
|
||||
@@ -91,10 +83,9 @@ function LoginPageClient({ siteName }: { siteName: string }) {
|
||||
}
|
||||
|
||||
export default function LoginPage() {
|
||||
const siteName = useSiteName();
|
||||
return (
|
||||
<Suspense>
|
||||
<LoginPageClient siteName={siteName} />
|
||||
<LoginPageClient />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import { usePathname, useRouter } from 'next/navigation';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
interface Props {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function AuthProvider({ children }: Props) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
|
||||
useEffect(() => {
|
||||
// 登录页或 API 路径不做校验,避免死循环
|
||||
if (pathname.startsWith('/login')) return;
|
||||
|
||||
const password = localStorage.getItem('password');
|
||||
const fullPath =
|
||||
typeof window !== 'undefined'
|
||||
? window.location.pathname + window.location.search
|
||||
: pathname;
|
||||
|
||||
// 有密码时验证
|
||||
(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
// 校验未通过,清理并跳转登录
|
||||
localStorage.removeItem('password');
|
||||
router.replace(`/login?redirect=${encodeURIComponent(fullPath)}`);
|
||||
}
|
||||
} catch (error) {
|
||||
// 网络错误等也认为未登录
|
||||
router.replace(`/login?redirect=${encodeURIComponent(fullPath)}`);
|
||||
}
|
||||
})();
|
||||
}, [pathname, router]);
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
@@ -2,13 +2,9 @@
|
||||
|
||||
import Link from 'next/link';
|
||||
|
||||
import { useSiteName } from './SiteNameContext';
|
||||
import { ThemeToggle } from './ThemeToggle';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
const MobileHeader = () => {
|
||||
const siteName = useSiteName();
|
||||
return (
|
||||
<header className='md:hidden relative w-full bg-white/70 backdrop-blur-xl border-b border-gray-200/50 shadow-sm dark:bg-gray-900/70 dark:border-gray-700/50'>
|
||||
<div className='h-12 flex items-center justify-center'>
|
||||
@@ -16,7 +12,7 @@ const MobileHeader = () => {
|
||||
href='/'
|
||||
className='text-2xl font-bold text-green-600 tracking-tight hover:opacity-80 transition-opacity'
|
||||
>
|
||||
{siteName}
|
||||
MoonTV
|
||||
</Link>
|
||||
</div>
|
||||
<div className='absolute top-1/2 right-4 -translate-y-1/2'>
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
'use client';
|
||||
|
||||
import {
|
||||
Clover,
|
||||
Film,
|
||||
@@ -24,8 +22,6 @@ import {
|
||||
useState,
|
||||
} from 'react';
|
||||
|
||||
import { useSiteName } from './SiteNameContext';
|
||||
|
||||
interface SidebarContextType {
|
||||
isCollapsed: boolean;
|
||||
}
|
||||
@@ -37,17 +33,13 @@ const SidebarContext = createContext<SidebarContextType>({
|
||||
export const useSidebar = () => useContext(SidebarContext);
|
||||
|
||||
// 可替换为你自己的 logo 图片
|
||||
interface LogoProps {
|
||||
siteName: string;
|
||||
}
|
||||
|
||||
const Logo = ({ siteName }: LogoProps) => (
|
||||
const Logo = () => (
|
||||
<Link
|
||||
href='/'
|
||||
className='flex items-center justify-center h-16 select-none hover:opacity-80 transition-opacity duration-200'
|
||||
>
|
||||
<span className='text-2xl font-bold text-green-600 tracking-tight'>
|
||||
{siteName}
|
||||
MoonTV
|
||||
</span>
|
||||
</Link>
|
||||
);
|
||||
@@ -64,8 +56,6 @@ declare global {
|
||||
}
|
||||
}
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
const Sidebar = ({ onToggle, activePath = '/' }: SidebarProps) => {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
@@ -168,8 +158,6 @@ const Sidebar = ({ onToggle, activePath = '/' }: SidebarProps) => {
|
||||
{ icon: VenetianMask, label: '日漫', href: '/douban?type=tv&tag=日本动画' },
|
||||
];
|
||||
|
||||
const siteName = useSiteName();
|
||||
|
||||
return (
|
||||
<SidebarContext.Provider value={contextValue}>
|
||||
{/* 在移动端隐藏侧边栏 */}
|
||||
@@ -193,7 +181,7 @@ const Sidebar = ({ onToggle, activePath = '/' }: SidebarProps) => {
|
||||
}`}
|
||||
>
|
||||
<div className='w-[calc(100%-4rem)] flex justify-center'>
|
||||
{!isCollapsed && <Logo siteName={siteName} />}
|
||||
{!isCollapsed && <Logo />}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import React, { createContext, useContext } from 'react';
|
||||
|
||||
/**
|
||||
* 站点名称上下文,默认值为 "MoonTV"。
|
||||
* 不包含 "use client",以便既能被 Server Component 引用,也能被 Client Component 引用。
|
||||
*/
|
||||
export const SiteNameContext = createContext<string>('MoonTV');
|
||||
|
||||
interface ProviderProps {
|
||||
value: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function SiteNameProvider({ value, children }: ProviderProps) {
|
||||
return (
|
||||
<SiteNameContext.Provider value={value}>
|
||||
{children}
|
||||
</SiteNameContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export const useSiteName = () => useContext(SiteNameContext);
|
||||
50
src/middleware.ts
Normal file
50
src/middleware.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
// 全站(含 /api)鉴权中间件,运行于 Edge Runtime。
|
||||
export async function middleware(req: NextRequest) {
|
||||
const { pathname, search } = req.nextUrl;
|
||||
|
||||
// 1. 放行无需鉴权的路径
|
||||
if (
|
||||
pathname.startsWith('/login') || // 登录页
|
||||
pathname.startsWith('/api/login') || // 登录接口
|
||||
pathname.startsWith('/_next') || // Next.js 静态文件
|
||||
pathname === '/favicon.ico' ||
|
||||
pathname.startsWith('/icons') ||
|
||||
pathname === '/manifest.json' ||
|
||||
pathname === '/logo.png' ||
|
||||
pathname === '/screenshot.png'
|
||||
) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 通过后端接口验证登录状态(GET /api/login)
|
||||
const origin = req.nextUrl.origin;
|
||||
const verifyRes = await fetch(`${origin}/api/login`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Cookie: req.headers.get('cookie') || '',
|
||||
},
|
||||
});
|
||||
|
||||
if (verifyRes.ok) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 未通过校验:API 返回 401,页面跳转登录
|
||||
if (pathname.startsWith('/api')) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const loginUrl = req.nextUrl.clone();
|
||||
loginUrl.pathname = '/login';
|
||||
loginUrl.searchParams.set('redirect', pathname + search);
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
|
||||
// 2. 指定哪些路径使用 middleware
|
||||
export const config = {
|
||||
matcher: [
|
||||
'/((?!_next/static|_next/image|favicon.ico|manifest.json|icons|logo.png|screenshot.png).*)',
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user