initial commit

This commit is contained in:
Ken Yasue
2025-03-25 06:19:44 +01:00
parent b97fa96c25
commit 9aef2ad891
71 changed files with 13016 additions and 1 deletions

65
src/middleware.ts Normal file
View File

@ -0,0 +1,65 @@
import { NextRequest, NextResponse } from 'next/server';
// Paths that don't require authentication
const publicPaths = ['/admin/login'];
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Add current path to headers for server components
const response = NextResponse.next({
request: {
headers: new Headers(request.headers),
},
});
response.headers.set('x-pathname', pathname);
// Only apply auth middleware to admin routes
if (!pathname.startsWith('/admin')) {
return response;
}
// Allow access to public paths
if (publicPaths.includes(pathname)) {
return response;
}
// Check for auth cookie
const authCookie = request.cookies.get('auth');
// If no auth cookie, redirect to login
if (!authCookie?.value) {
const url = new URL('/admin/login', request.url);
return NextResponse.redirect(url);
}
// Verify the auth cookie by calling the auth API
try {
const authResponse = await fetch(new URL('/api/auth', request.url), {
headers: {
Cookie: `auth=${authCookie.value}`,
},
});
const data = await authResponse.json();
if (!data.authenticated) {
const url = new URL('/admin/login', request.url);
return NextResponse.redirect(url);
}
} catch (error) {
console.error('Auth verification error:', error);
const url = new URL('/admin/login', request.url);
return NextResponse.redirect(url);
}
return response;
}
// Configure middleware to run on all routes
export const config = {
matcher: [
// Apply to all routes
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
};