I currently have a CORS problem that I can't solve, here is my code:
gogologo.js
const souscription = async (req, res) => {
if (!req.body.data || !req.body.data.souscrireParams) {
console.error('souscription input error')
res.json({ data: { error: 'souscription input error' } })
return
}
const souscrireParams = req.body.data.souscrireParams
console.log('souscription:', JSON.stringify(req.body.data.souscrireParams))
var data = JSON.stringify(souscrireParams)
const headers = {
'x-api-key': apiKey,
'Content-Type': 'application/json'
}
var reqConfig = {
method: 'post',
url: 'url',
headers: headers,
data: data
};
console.log("reqConfig = ", reqConfig)
try {
const response = await axios(reqConfig)
const result = response.data
if (result.resultat === 'success') {
res.json({ data: result })
} else {
const msg = {
to: 'mail',
from: 'mail',
subject: 'souscription Fail',
html: `deal <br><br> ${JSON.stringify(souscrireParams)}<br><br>Error:<br><br>`,
}
sgMail.setApiKey(sgKey)
await sgMail.send(msg)
res.json({ data: { error: { message: 'souscrire error' } } })
}
} catch (error) {
console.log('error message : ', error.message)
console.log('error : ', error)
const msg = {
to: 'mail',
from: 'mail',
subject: 'souscription fail',
html: `deal <br><br> ${JSON.stringify(souscrireParams)}<br><br>Error:<br><br>${JSON.stringify({ error: error.message })}`,
}
sgMail.setApiKey(sgKey)
await sgMail.send(msg)
res.json({ data: { error: error.message } })
}
}
exports.souscription = souscription
index.js
exports.souscription = functions.runWith({
vpcConnector: 'app-functions-connector',
vpcConnectorEgressSettings: 'ALL_TRAFFIC'
}).https.onRequest(async (req, res) => {
console.log('souscription IP:', await publicIp.v4())
cors(req, res, async () => {
gogologo.souscription(req, res)
})
})
when I press the subscribe button, it displays an error message "Access to fetch at 'url genereted by firebase function' from origin 'url app' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled." with a post error.