I want to use Middleware with Database strategy.

Hi, I'd like to use the nextjs middleware using the database sessions strategy for next auth. In the cookies we get the property 'next-auth.session-token', which contains a value of the session's id stored in my database. I could theoretically do a lookup in my DB if this session exists and is linked to a valid user. I'd like some opinions on this, is this a good approach on it? My usecase is securing an admin page for a website, of which I need to acess the user's information in the user table.
export function middleware(req: NextRequest) {
const session = req.cookies.get('next-auth.session-token');
// Here I could do a call to my API endpoint which will perform a lookup on my database
}
export function middleware(req: NextRequest) {
const session = req.cookies.get('next-auth.session-token');
// Here I could do a call to my API endpoint which will perform a lookup on my database
}
Is there a better solution than what i'm currently thinking?
1 Reply
Scot
Scot17mo ago
this is the "standard" practice, howver it does involve a round trip to the db which has performant impacts your api is in a seperate location (ie running on the edge and your db is in a data center)
Want results from more Discord servers?
Add your server