fix: use constant for cookie value and remove unused import
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,3 +5,4 @@ export const AUTH_USERNAME = process.env.AUTH_USERNAME || 'admin';
|
|||||||
export const AUTH_PASSWORD = process.env.AUTH_PASSWORD || 'changeme'; // Change in production!
|
export const AUTH_PASSWORD = process.env.AUTH_PASSWORD || 'changeme'; // Change in production!
|
||||||
export const COOKIE_NAME = 'auth';
|
export const COOKIE_NAME = 'auth';
|
||||||
export const COOKIE_MAX_AGE = 86400; // 24 hours
|
export const COOKIE_MAX_AGE = 86400; // 24 hours
|
||||||
|
export const AUTH_COOKIE_VALUE = 'ok';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// middleware.ts
|
// middleware.ts
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import type { NextRequest } from 'next/server';
|
import type { NextRequest } from 'next/server';
|
||||||
import { COOKIE_NAME, COOKIE_MAX_AGE } from './lib/constants';
|
import { COOKIE_NAME, AUTH_COOKIE_VALUE } from './lib/constants';
|
||||||
|
|
||||||
export function middleware(request: NextRequest) {
|
export function middleware(request: NextRequest) {
|
||||||
const { pathname } = request.nextUrl;
|
const { pathname } = request.nextUrl;
|
||||||
@@ -14,7 +14,7 @@ export function middleware(request: NextRequest) {
|
|||||||
// 检查是否有认证 cookie
|
// 检查是否有认证 cookie
|
||||||
const authCookie = request.cookies.get(COOKIE_NAME);
|
const authCookie = request.cookies.get(COOKIE_NAME);
|
||||||
|
|
||||||
if (!authCookie || authCookie.value !== 'ok') {
|
if (!authCookie || authCookie.value !== AUTH_COOKIE_VALUE) {
|
||||||
// 未认证,重定向到登录页
|
// 未认证,重定向到登录页
|
||||||
const loginUrl = new URL('/login', request.url);
|
const loginUrl = new URL('/login', request.url);
|
||||||
return NextResponse.redirect(loginUrl);
|
return NextResponse.redirect(loginUrl);
|
||||||
|
|||||||
Reference in New Issue
Block a user