JWT Token verification in Middleware
Unanswered
Ferruginous Hawk posted this in #help-forum
Ferruginous HawkOP
My app is set up to create and store (access and refresh) cookies for signed in user. However, when I try to verify cookies in my Next.js middleware, I am getting some errors.
First, I used my custom
For the former, I get error that
And for the latter,
Question: How to best handle tokens and cookies in Next.js middleware.
First, I used my custom
verifyToken
function, and then I directy import jwt from "jsonwebtoken"
.For the former, I get error that
Error in middleware: TypeError: Function has non-object prototype 'undefined' in instanceof check
And for the latter,
Error in middleware: Error: The edge runtime does not support Node.js 'crypto' module.
Question: How to best handle tokens and cookies in Next.js middleware.
2 Replies
@Ferruginous Hawk My app is set up to create and store (access and refresh) cookies for signed in user. However, when I try to verify cookies in my Next.js middleware, I am getting some errors.
First, I used my custom `verifyToken` function, and then I directy `import jwt from "jsonwebtoken"`.
For the former, I get error that `Error in middleware: TypeError: Function has non-object prototype 'undefined' in instanceof check`
And for the latter, `Error in middleware: Error: The edge runtime does not support Node.js 'crypto' module.`
Question: How to best handle tokens and cookies in Next.js middleware.
Sun bear
Either use the npm package Jose or change the runtime of the middleware to node.js (only working in canary afaik)
Ferruginous HawkOP
Yeah. Refactoring my code to use José instead of jsonwebtoken.