Nextjs | Set cookies header
Hi, I want to make a call to an API (which I don't have access to) that returns headers with cookies, however the cookies are blocked since the host domain is different. I tried to use rewrite in the config file but it did not change anything, does anyone have a solution?
30 Replies
@Zepeto
Sounds like you are having a CORS issue, do you have any control over the backend API whatsoever?
You have a few options you can try:
1. Use a proxy: One solution is to set up a server-side proxy that makes the API request on behalf of your app. The proxy can then forward the API response back to your app, and include any cookies in the response headers. Since the API request is made from the same domain as your app, the browser will allow the cookies to be set. You can use a library like http-proxy-middleware to set up the proxy in your Next.js app.
2. Use JSON Web Tokens (JWTs): If the API supports JWT authentication, you can use JWTs to authenticate your requests instead of cookies. JWTs are tokens that are signed by the server and can be used to authenticate subsequent requests. You can store the JWT in local storage or a cookie that is accessible to your app's domain, and include the JWT in your API requests.
3. Use CORS headers: If you have access to the API server, you can set the appropriate CORS headers to allow your app's domain to access the API. CORS headers tell the browser to allow cross-origin requests from specific domains. You can set the Access-Control-Allow-Origin header to your app's domain to allow cross-origin requests, and include the Access-Control-Allow-Credentials header to allow cookies to be sent with the request.
No access to the api
I used rewrite config in config file but Iβm unauthorized since I use it this way
Yeah, without more detailed info about the API, it's hard to come up with more specific suggestions, but I would guess #1 is your best bet
You could, for example, try nextjs-cors or cors-anywhere.
GitHub
GitHub - Rob--W/cors-anywhere: CORS Anywhere is a NodeJS reverse pr...
CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request. - GitHub - Rob--W/cors-anywhere: CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxi...
GitHub
GitHub - yonycalsin/nextjs-cors: nextjs-cors is a node.js package t...
:tada: nextjs-cors is a node.js package to provide a Connect/Express middleware that can be used to enable CORS with various options :rocket: - GitHub - yonycalsin/nextjs-cors: nextjs-cors is a nod...
I can't use native proxy of Nextjs ?
The native proxy feature of Next.js does not handle CORS headers automatically. @Zepeto
But you can try setting it up in next config:
it's finally working but not locally ahahah I have to deploy it on Vercel to see real result, it looks like http requests are subject to a CloudFlare test
So I have to add this package to my app ?
I don't have CORS problemes anymore
No no, just call that directly, setup a local .env and use http://cors-anywhere.herokuapp.com/http://api-url.com or something
I now have a certificate problem, I'm just looking for a way to be in https locally so I don't have to deploy each time the app on vercel to test it
Are you developing on Windows?
yes
Here's how to set up a self-signed SSL certificate for your Next.js app:
- Install the mkcert utility by following the instructions in the mkcert README. mkcert is a utility that allows you to create self-signed SSL certificates.
- Run mkcert -install to install the local CA (Certificate Authority) that mkcert uses to sign certificates.
- In your Next.js project directory, run the following command to create a self-signed SSL certificate:
mkcert -key-file key.pem -cert-file cert.pem localhost 127.0.0.1 ::1
This will create a private key file (key.pem) and a certificate file (cert.pem) in your project directory. The certificate is signed by the local CA installed in step
Update your Next.js server configuration to use the SSL certificate. You can do this by adding the following code to your next.config.js file:
GitHub
GitHub - FiloSottile/mkcert: A simple zero-config tool to make loca...
A simple zero-config tool to make locally trusted development certificates with any names you'd like. - GitHub - FiloSottile/mkcert: A simple zero-config tool to make locally trusted develo...
I got this error when I start the app
ReferenceError: fs is not defined
Fs is a serverside Node.js package, are you calling it inside your client code?
nop I just copied the module export
Here's an example of how to use the fs module in your next.config.js file:
Install the fs-extra package by running npm install fs-extra or yarn add fs-extra.
In your next.config.js file, import the fs-extra package and use it to read the SSL certificate files:
Make sure to adjust the path to the certificate files based on your project structure.
Use the webpack property to customize the Next.js webpack configuration:
You sure about that ?
I haven't personally tested the code, but it should work
error - Failed to load next.config.js
Probably a typo or syntax error
I just copied what you gave me
Check there isn't any red squigglies or errors in your editor
Nothing in editor
You replaced your existing config with the code?
nop just added to it
but it was almost empty
hmmm, might require some debugging to fix, but if I were you I'd consider if you could just use http instead of https locally
Yes but the api blocks the request with cloudflare