Hii I have built a netlify function to get the JWT token
"Access to fetch at 'function URL ’ from origin ‘localhost’ 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.
I have added the headers in netlify.toml & in my code as well but still its not working
this is my js code
// Place this code in a file within your project's Netlify functions directory.
// For example, if your function's name is generate-token, the file path might be: /netlify/functions/generate-token.js
const jwt = require("jsonwebtoken");
const { v4: uuidv4 } = require("uuid");
exports.handler = async (event) => {
// Only allow POST requests
if (event.httpMethod !== "POST") {
return {
statusCode: 405,
body: JSON.stringify({ error: "Method Not Allowed" }),
};
}
// Function to generate JWT
const generateJwt = () => {
const secret = process.env.Tableau_secret;
const secretId = process.env.Tableau_secretId;
const clientId = process.env.Tableau_clientId;
const scopes = ["tableau:views:embed", "tableau:views:embed_authoring"];
const userId =process.env.Tableau_email;
const tokenExpiryInMinutes = 1; // Max of 10 minutes.
const userAttributes = {
// User attributes are optional. Add entries to this dictionary if desired.
};
const header = {
alg: "HS256",
typ: "JWT",
kid: secretId,
iss: clientId,
};
const data = {
jti: uuidv4(),
aud: "tableau",
sub: userId,
scp: scopes,
exp: Math.floor(Date.now() / 1000) + tokenExpiryInMinutes * 60,
...userAttributes,
};
return jwt.sign(data, secret, { header });
};
// Generate the token
const token = generateJwt();
// Return the token in the response
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
"Access-Control-Allow-Methods": "POST, OPTIONS",
},
body: JSON.stringify({ token }),
};
};
and this is toml file
[build]
functions = “netlify/functions”
[[headers]]
for = “/” # Targeting a specific function
[headers.values]
Access-Control-Allow-Origin = “*”
Access-Control-Allow-Methods = “POST, OPTIONS”
Access-Control-Allow-Headers = “Content-Type”