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:
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?