App Router - How to wrap a layout in session to require login?
I want to force all users to be logged in to access a section of the site using a certain layout. For testing purposes I want the top level layout in the app directory to check for an existing session and redirect to sign in if no session exists.
The next auth docs seem to be for the pages directory and those methods didn't seem to work.
Is there a guide to do this with the app directory that I missed on the next auth pages?
I've read that middleware might be the way to go here, with match on the routes where it is to apply. Is there an example of database session middleware to achieve this?
If not, how can I modify the start template layout (below) to check for session server side?
Solution:Jump to solution
I ended up using the following approach, it works well enough it seems:
``` // added the following import
import { getServerAuthSession } from "~/server/auth";
...
1 Reply
Solution
I ended up using the following approach, it works well enough it seems: