Consuming API's created by nextjs
Consuming API's create by nextjs in flutter but getting this error
Check no 'Access-Control-Allow-Origin' header is present on the requested resource. Below is the source code of my next.config.js
Also my route handler I'm consuming
/** @type {import('next').NextConfig} */
const nextConfig = {
async headers() {
return [
{
// matching all API routes
source: "/api/:path*",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{ key: "Access-Control-Allow-Origin", value: "*" }, // replace this your actual origin
{ key: "Access-Control-Allow-Methods", value: "GET,DELETE,PATCH,POST,PUT" },
{ key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" },
]
}
]
}
}
module.exports = nextConfig
/** @type {import('next').NextConfig} */
const nextConfig = {
async headers() {
return [
{
// matching all API routes
source: "/api/:path*",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{ key: "Access-Control-Allow-Origin", value: "*" }, // replace this your actual origin
{ key: "Access-Control-Allow-Methods", value: "GET,DELETE,PATCH,POST,PUT" },
{ key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" },
]
}
]
}
}
module.exports = nextConfig
const response = {
message: "Authenticated!",
};
const seralized = serialize(COOKIE_NAME, JSON.stringify(userData), {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "strict",
maxAge: MAX_AGE,
path: "/",
});
return new Response(JSON.stringify(response), {
status: 200,
headers: {
'Access-Control-Allow-Origin':'*',
'Content-Type': 'application/json',
"Set-Cookie": seralized
},
});
const response = {
message: "Authenticated!",
};
const seralized = serialize(COOKIE_NAME, JSON.stringify(userData), {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "strict",
maxAge: MAX_AGE,
path: "/",
});
return new Response(JSON.stringify(response), {
status: 200,
headers: {
'Access-Control-Allow-Origin':'*',
'Content-Type': 'application/json',
"Set-Cookie": seralized
},
});
0 Replies