Next.js Discord

Discord Forum

Can i connect to db in middleware to verify token?

Unanswered
Auth posted this in #help-forum
Open in Discord
should i connect to db to check if token exists on db or is there any other way

4 Replies

Brown bear
You should use JWT.
connecting to database and then longer round trips in middleware is kinda not that recommended as its meant to be fast (also its edge runtime and cant use most db libs)
so id say you can check if it exists there, and do actual checks in the page.tsx
if you use jwt like Shamim3l, you can check if its "valid" in middleware, just be aware that its not for all cases (ie cant be revoked as its stateless)