d4c82867d4
/api/settings was missing from the middleware public routes allowlist, causing unauthenticated (guest) requests to be blocked before reaching the route handler. The error was silently caught, leaving settings null and hiding the amount owed, payment methods, and payment instructions. Logged-in users were unaffected as their session token passed middleware. Also update CLAUDE.md to reflect the WebSocket userId-based auth change. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { withAuth } from 'next-auth/middleware';
|
|
import { NextResponse } from 'next/server';
|
|
|
|
export default withAuth(
|
|
function middleware(req) {
|
|
const { pathname } = req.nextUrl;
|
|
const token = req.nextauth.token;
|
|
|
|
// Admin routes require ADMIN or VIEWER role
|
|
if (pathname.startsWith('/admin')) {
|
|
if (token?.role !== 'ADMIN' && token?.role !== 'VIEWER') {
|
|
return NextResponse.redirect(new URL('/login', req.url));
|
|
}
|
|
}
|
|
|
|
// My-squares requires PLAYER or higher
|
|
if (pathname.startsWith('/my-squares')) {
|
|
if (!token?.role) {
|
|
return NextResponse.redirect(new URL('/login', req.url));
|
|
}
|
|
}
|
|
|
|
return NextResponse.next();
|
|
},
|
|
{
|
|
callbacks: {
|
|
authorized: ({ token, req }) => {
|
|
const { pathname } = req.nextUrl;
|
|
// Allow public routes without auth
|
|
if (
|
|
pathname === '/' ||
|
|
pathname === '/login' ||
|
|
pathname === '/register' ||
|
|
pathname === '/signup' ||
|
|
pathname === '/setup' ||
|
|
pathname.startsWith('/api/auth') ||
|
|
pathname.startsWith('/api/setup') ||
|
|
pathname.startsWith('/api/squares') ||
|
|
pathname.startsWith('/api/settings') ||
|
|
pathname.startsWith('/api/users') ||
|
|
pathname.startsWith('/_next') ||
|
|
pathname.startsWith('/images')
|
|
) {
|
|
return true;
|
|
}
|
|
// All other routes require authentication
|
|
return !!token;
|
|
},
|
|
},
|
|
}
|
|
);
|
|
|
|
export const config = {
|
|
matcher: [
|
|
'/((?!_next/static|_next/image|favicon.ico|images).*)',
|
|
],
|
|
};
|