Next.js Discord

Discord Forum

authjs session not working with middleware

Unanswered
mihir posted this in #help-forum
Open in Discord
When setting custom properties and calling the session in any page using auth()
function it works just fine but when accessed using req.auth.user property
all the set properties of user object that were set using jwt token becomes undefined even though typescript recognizes it

8 Replies

Sun bear
Do you set the custom probs via database query that maybe doesnt work on edge?
database call
in the jwt call back
then from that to session
but i just figured that middleware runs before auth.ts
but still dont understand how it has the 3 values, name email and image
even id is undefined
so for now the workaround i have is to call in layout itself
Sun bear
I would make the db call in token() in a try catch so that the middleware doesnt receive undefined

And then

session(session, token) {
session.id = token.id
// Other keys
return session
}


Name, email and image are part of the original session after signup