Hello! I'm looking for some help with nextjs and m...
# support-questions-legacy
u
Hello! I'm looking for some help with nextjs and middleware integration. I've followed the example in the docs here: https://supertokens.com/docs/thirdpartyemailpassword/nextjs/init And have that working pretty well. I want to refactor some of it to make it more dry but my attempts so far haven't worked. in the
pages/api/user.js
file it has the following code:
Copy code
supertokens.init(backendConfig())
export default async function user(req, res) {
  await superTokensNextWrapper(
    async (next) => {
      return await verifySession()(req, res, next)
    },
    req,
    res
  )
  return res.json({
    note: "Fetch any data from your application for authenticated user after using verifySession middleware",
    userId: req.session.getUserId(),
    sessionHandle: req.session.getHandle(),
    accessTokenPayload: req.session.getAccessTokenPayload(),
  });
}
The part where it checks calls
verifySession
I would like to move into a middleware that is called for every api in the
/api/*
file path. I thought I could just move that verifySession call into a another function and nextjs handles when it is invoked, but I'm not having much luck. Is there a way to do with with Supertokens or does the verify session need to be called directly in every api route?
r
hey
u
hey sir! I'm not sure if this is more a supertokens issue or a nextjs issue. I'm not convinced that my middleware is being called actually.
r
oh.. Can you add a console log in there and check if the middleware is called?
if it doesn't work, consider opening a github issue about it on our repo and we can have a look at it in a few days.
u
I have a console log in the middleware and I don't see it printing out on the server.
Sounds good
r
Right. That’s probably a NextJS issue then