SupabaseS
Supabase4y ago
deed

How to get current user data server side (next.js middleware)

Hi,

I'm trying to make a nextjs middleware function that requires getting the current session on the server. This data is there client side and in local storage, but how do I access it via the server to use my middleware function?

I was hoping to do something like this:

export async function middleware(request: NextRequest) {
  const {data} = await supabase.auth.getSession()
  if (data) {
    return NextResponse.redirect(new URL("/", request.url));
  } else {
    return NextResponse.redirect(new URL("/unauthorized", request.url));
  }
}
Was this page helpful?