Next.js Discord

Discord Forum

NextJS CORS Error

Unanswered
Tramp ant posted this in #help-forum
Open in Discord
Tramp antOP
Hi All, When I am connecting to my springboot backend with my NextJS frontend it seems to give me a CORS error anyone know how to fix this or proxy the request? This is 100% frontend issue

2 Replies

Tramp antOP
export async function fetchUserTokenFromUserBackend(base64Token: string) {
  
  const url = new URL(
    `${process.env.NEXT_PUBLIC_BASE_URL}${process.env.NEXT_PUBLIC_POST_LOGIN_PATH}`
  );

  const options: RequestInit = {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Basic ${base64Token}`,
    },
  };
  const response = await fetch(url, options);

  if (!response.ok) {
    throw new Error('Network response was not ok');
  }

  const data = await response.json();
  return data.token;
}
I know rewrites exist like this below but it doesn't seem to work with the above
 
module.exports = {
  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: 'http://localhost:8000/:path*' // Proxy to Backend
      }
    ]
  }
}