What is the best approach when handling the protected and unprotected routes
I designed a layout for the
sign in
and sign up
pages with the goal of redirecting any authenticated user straight to the home page (/
). However, when I navigate to either the sign in or sign up page while already having an active session, the page content briefly renders before the redirect occurs. Additionally, I encounter the following error:
Here is the relevant code:
Solution:Jump to solution
```
import { headers } from 'next/headers';
import { redirect } from 'next/navigation';
import { auth } from '@/lib/auth';...
5 Replies
Solution
you are doing CSR(client side rendering) with 'use client' , you have to move that up to do with RSC along with
auth.api.getSession()
or maybe the best approach is create a
middleware.ts
in the next.js
?yeah but middleware in nextjs aint pass the user / session context to the each of route segments. but for guarding middleware is good choice