mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-02-24 12:24:46 +08:00
@@ -24,41 +24,6 @@ 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,6 +3,7 @@ import { Inter } from 'next/font/google';
|
||||
|
||||
import './globals.css';
|
||||
|
||||
import AuthProvider from '../components/AuthProvider';
|
||||
import { ThemeProvider } from '../components/ThemeProvider';
|
||||
|
||||
const inter = Inter({ subsets: ['latin'] });
|
||||
@@ -37,7 +38,7 @@ export default function RootLayout({
|
||||
enableSystem
|
||||
disableTransitionOnChange
|
||||
>
|
||||
{children}
|
||||
<AuthProvider>{children}</AuthProvider>
|
||||
</ThemeProvider>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -27,6 +27,11 @@ function LoginPageClient() {
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
// 保存密码以供后续请求使用
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('password', password);
|
||||
}
|
||||
|
||||
const redirect = searchParams.get('redirect') || '/';
|
||||
router.replace(redirect);
|
||||
} else if (res.status === 401) {
|
||||
|
||||
Reference in New Issue
Block a user