Refresh Session Cookie for Next.js Server Components with Middleware

Share this video with your friends

Send Tweet

Server Components cannot set cookies in the Next.js App Router. In this lesson, we implement middleware to refresh expired Supabase sessions.

Middleware runs immediately before the route is loaded. By using it to refresh our user's session, we ensure it is valid by the time it loads the Server Component and attempts to fetch data from Supabase.

Code Snippets

Refresh expired sessions

supabase.auth.getSession();

Resources