auto-claude: subtask-1-2 - Add authentication check to middleware
This commit is contained in:
+29
-1
@@ -1,12 +1,40 @@
|
|||||||
import createMiddleware from 'next-intl/middleware';
|
import createMiddleware from 'next-intl/middleware';
|
||||||
import { locales, defaultLocale } from './i18n/config';
|
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,
|
locales,
|
||||||
defaultLocale,
|
defaultLocale,
|
||||||
localePrefix: 'always',
|
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 = {
|
export const config = {
|
||||||
matcher: [
|
matcher: [
|
||||||
'/',
|
'/',
|
||||||
|
|||||||
Reference in New Issue
Block a user