Revert "feat: use middleware to auth"

This reverts commit f57cdb5ec1.
This commit is contained in:
shinya
2025-06-30 23:17:05 +08:00
parent afef6d9865
commit 240764e81b
5 changed files with 53 additions and 92 deletions

View File

@@ -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 });

View File

@@ -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>

View File

@@ -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) {