import { createI18nMiddleware } from 'next-international/middleware';
import { NextRequest, NextResponse } from 'next/server';
import { getMe } from './api/auth';
import { cookies } from 'next/headers';
import { typedFetchJSON } from './utils/typedFetch';
import { User } from './types/auth';
const locales = ['ru', 'tm', 'en', 'tr'];
const I18nMiddleware = createI18nMiddleware({
locales,
defaultLocale: 'ru',
});
export async function middleware(request: NextRequest) {
const protectedPaths = ['/profile', '/add-ads'];
if (protectedPaths.includes(request.nextUrl.pathname.slice(3))) {
try {
const response = function getMe() {
return typedFetchJSON<User>(
`${apiUrl}/user/me`,
{
headers: { Cookie: cookies().toString() },
credentials: 'include',
}
);
};
if (response?.type !== 'success') {
console.log('response getMe');
return NextResponse.redirect(new URL('/sign-in', request.url));
}
} catch (error) {
console.error('Failed to fetch user profile:', error);
}
}
return I18nMiddleware(request);
}
export const config = {
matcher: [
'/((?!api|static|.*\\..*|_next|favicon.ico|robots.txt|serviceworker.js).*)',
],
};
export function getMe() {
return typedFetchJSON<User>(`https://portal1.tp-projects.com/api/user/me`, {
headers: {
Cookie: cookieValue,
},
credentials: 'include',
});
}