0

I'm trying to implement a simple next-auth middleware to protect all api routes except a /api/healthcheck route.

I wrote the following within /pages/api/_middleware

import { withAuth } from 'next-auth/middleware';

// protect all api routes but /api/healthcheck
export default withAuth({
  callbacks: {
    authorized: async ({ req, token }) => {
      if (req.page.name === '/api/healthcheck') return true;
      if (token) return true;
      return false;
    },
  },
});

This seems to work correctly on localhost but when I try to build I get the following error

Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware pages/api/_middleware

Any ideas what can cause this?

juliomalves
  • 42,130
  • 20
  • 150
  • 146

1 Answers1

0

Figured it out.

I mistaken made the authorized callback async which apparently is forbidden