middleware.ts (687B)
1 import { NextResponse } from 'next/server'; 2 3 import type { NextRequest } from 'next/server'; 4 5 export const config = { 6 matcher: ['/console/:path*'], 7 }; 8 9 // TODO: 認証失敗時のフィードバックを改善 10 export function middleware(req: NextRequest) { 11 const basicAuth = req.headers.get('authorization'); 12 const url = req.nextUrl; 13 14 if (basicAuth) { 15 const authValue = basicAuth.split(' ')[1] ?? ''; 16 17 const [user, pwd] = Buffer.from(authValue, 'base64').toString().split(':'); 18 19 if ( 20 user === process.env['ADMIN_USER'] && 21 pwd === process.env['ADMIN_PASSWORD'] 22 ) { 23 return NextResponse.next(); 24 } 25 } 26 27 url.pathname = '/api/auth'; 28 return NextResponse.rewrite(url); 29 }