fix: client-side auth check instead of middleware rewrite
All checks were successful
Deploy / deploy (push) Successful in 2m38s

This commit is contained in:
Cosmo
2026-04-22 19:19:33 +00:00
parent c7fc4d6e8e
commit 4e4d434c0b
3 changed files with 30 additions and 27 deletions

View File

@@ -13,12 +13,8 @@ async function hmacSha256(secret: string, message: string): Promise<string> {
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
if (
pathname.startsWith('/api/auth') ||
pathname.startsWith('/_next') ||
pathname.startsWith('/favicon') ||
pathname === '/manifest.json'
) {
// Only protect API routes (except /api/auth)
if (!pathname.startsWith('/api/') || pathname.startsWith('/api/auth')) {
return NextResponse.next()
}
@@ -28,17 +24,12 @@ export async function middleware(request: NextRequest) {
const expectedToken = await hmacSha256(secret, pin)
if (token !== expectedToken) {
if (pathname.startsWith('/api/')) {
return NextResponse.json({ error: 'unauthorized' }, { status: 401 })
}
const url = request.nextUrl.clone()
url.searchParams.set('locked', '1')
return NextResponse.rewrite(url)
return NextResponse.json({ error: 'unauthorized' }, { status: 401 })
}
return NextResponse.next()
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico|manifest.json).*)'],
matcher: ['/api/:path*'],
}