Updating user profile ManagementClient: Bad Request: Bad HTTP authentication header format

Hi,

Having some difficulty updating user email using the ManagementClient. My environment is NextJS 13. My form is getting the data sent correctly to the api, but I’m receiving a “Bad Request: Bad HTTP authentication header format”.

page.tsx

const updateProfile = async (values) => {
  const res = await fetch(`/api/updateProfile`, {
    method: 'PATCH',
    body: JSON.stringify({ values }),
    headers: { 'Content-Type': 'application/json' },
  });
  return await res.json();
};

updateProfile.ts

import { withApiAuthRequired, getSession } from '@auth0/nextjs-auth0';
var ManagementClient = require('auth0').ManagementClient;

export default withApiAuthRequired(async function handler(req, res) {
  const session = getSession(req, res);

  const management = new ManagementClient({
    domain: 'dev-i3fn082o3hvn2k18.us.auth0.com',
    clientId: process.env.AUTH0_CLIENT_ID,
    clientSecret: process.env.AUTH0_CLIENT_SECRET,
    scope: 'update:users',
    token: (await session).accessToken,
    // audience: 'https://dev-i3fn082o3hvn2k18.us.auth0.com/api/v2/',
  });

  const { email, id } = req.body.values;
  const params = { id };
  const data = { email: email };

  try {
    await management.updateUser(params, data);
    res.status(200).json({ message: 'User updated' });
  } catch (e) {
    console.error(e);
    res.status(500).json({ message: 'Error updating user' });
  }
});

Not sure where I’ve gone wrong here. Thanks for any guidance.