Middleware, i18n and supabase auth, how can I make this work together?
Unanswered
thomasschwinn posted this in #help-forum
hi,
I have an nextjs app with the app router and nicely configured for i18n. Everything works fine.
Now I want to add supabase auth, but I have no idea how and where in middleware I can add the supabase code, so it works.
This is my middelware for the i18n configuration:
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (pathname.includes("favicon")) {
return;
}
// if (pathname.includes("favicons")) {
// return NextResponse.redirect(new URL(pathname.replace("/de", "")));
// }
// Check if there is any supported locale in the pathname
const pathnameIsMissingLocale = i18n.locales.every(
(locale) => !pathname.startsWith(
);
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
const locale = getLocale(request);
//console.log(request.url);
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(
new URL(
);
}
}
and this is the supabase code I should add to middleware, but I have no idea how I can integrate it:
export async function middleware(req: NextRequest) {
const res = NextResponse.next()
// Create a Supabase client configured to use cookies
const supabase = createMiddlewareClient({ req, res })
// Refresh session if expired - required for Server Components
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
await supabase.auth.getSession()
return res
}
I'm greatful for any tipp how to do this. Thank you
I have an nextjs app with the app router and nicely configured for i18n. Everything works fine.
Now I want to add supabase auth, but I have no idea how and where in middleware I can add the supabase code, so it works.
This is my middelware for the i18n configuration:
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (pathname.includes("favicon")) {
return;
}
// if (pathname.includes("favicons")) {
// return NextResponse.redirect(new URL(pathname.replace("/de", "")));
// }
// Check if there is any supported locale in the pathname
const pathnameIsMissingLocale = i18n.locales.every(
(locale) => !pathname.startsWith(
/${locale}/) && pathname !== /${locale});
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
const locale = getLocale(request);
//console.log(request.url);
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(
new URL(
/${locale}/${pathname}, request.url));
}
}
and this is the supabase code I should add to middleware, but I have no idea how I can integrate it:
export async function middleware(req: NextRequest) {
const res = NextResponse.next()
// Create a Supabase client configured to use cookies
const supabase = createMiddlewareClient({ req, res })
// Refresh session if expired - required for Server Components
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
await supabase.auth.getSession()
return res
}
I'm greatful for any tipp how to do this. Thank you