Correct way to get cookie from the rest api

Hello, I am wondering what's the correct place to get csrftoken from backend rest api and send to the user? I use ssr, so I want to make it on server side. So in my attempt I created server/api/csrftoken.ts where I get the token from backend, then created server/middleware/auth.ts with the following content:
export default defineEventHandler(async (event) => {
  const { csrftoken } = await $fetch('/api/csrftoken');

  setCookie(event, 'csrftoken', csrftoken);
});


But it doesn't work, I got 500 error from the Nitro. Maybe there is a correct way?
Was this page helpful?