auto-claude: subtask-1-2 - Add authentication check to middleware

This commit is contained in:
2026-01-25 06:37:28 +01:00
parent 44fd4880f9
commit ca415f346b
+29 -1
View File
@@ -1,12 +1,40 @@
import createMiddleware from 'next-intl/middleware';
import { locales, defaultLocale } from './i18n/config';
import { createClient } from '@/lib/supabase/middleware';
import { NextRequest, NextResponse } from 'next/server';
export default createMiddleware({
const intlMiddleware = createMiddleware({
locales,
defaultLocale,
localePrefix: 'always',
});
export async function middleware(request: NextRequest) {
// Check if the request is for a protected dashboard route
const pathname = request.nextUrl.pathname;
const isDashboardRoute = pathname.match(/^\/(de|en|sr)\/dashboard/);
if (isDashboardRoute) {
// Create Supabase client and check authentication
const { supabase, response } = await createClient(request);
const { data: { user } } = await supabase.auth.getUser();
// If not authenticated, redirect to login page with locale preserved
if (!user) {
const locale = pathname.split('/')[1];
const loginUrl = new URL(`/${locale}/login`, request.url);
loginUrl.searchParams.set('returnUrl', pathname);
return NextResponse.redirect(loginUrl);
}
// User is authenticated, continue with the response from Supabase client
return response;
}
// For non-dashboard routes, use the intl middleware
return intlMiddleware(request);
}
export const config = {
matcher: [
'/',