© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y 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));
  }
}
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));
  }
}
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Get user.id server-side in Next.js
SupabaseSSupabase / help-and-questions
4y ago
Get user server-side on supabase js 2.0 + next.js
SupabaseSSupabase / help-and-questions
4y ago
Get session data server side
SupabaseSSupabase / help-and-questions
4y ago
How to get user or session info on server side?
SupabaseSSupabase / help-and-questions
4y ago