I am experiencing some cors error while using fastify and firebase emulator locally:
signin:1 Access to fetch at 'http://127.0.0.1:5001/<redacted>/asia-southeast1/hello/api/v1/session_login' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.
Fastify Backend
server.register(cors, {
origin: true,
methods: ["GET", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"],
credentials: true,
});
server.register(singleClientAndContact, { prefix: API_V1 });
exports.hello = onRequest(
{
secrets: [DB_USER, DB_PASSWORD],
cors: true,
region: "asia-southeast1",
},
async (req, res) => {
await server.ready();
server.server.emit("request", req, res);
}
);
React frontend:
const handleGoogle = async () => {
const userCredential = await signInWithEmailAndPassword(
auth,
username,
password
);
const idToken = await getIdToken(userCredential.user);
const res = await postIdTokenToSessionLogin(idToken);
setUsername("");
setPassword("");
if (res.ok) {
return navigate("/client_management", { replace: true });
}
};
async function postIdTokenToSessionLogin(idToken: string) {
return await fetch(`${DOMAIN}/api/v1/session_login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ idToken }),
credentials: "include",
});
}
What I have tried
I have also set the firebase function’s permissions to allUsers
for the Cloud Invoker Function permissions.
I have also initialized firebase to connect to the emulator:
// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
const functions = getFunctions(getApp(), "asia-southeast1");
setPersistence(auth, browserLocalPersistence);
///questions/75264880/login-persistance-in-firebase-v9
connectFunctionsEmulator(functions, "localhost", 5001);
I noticed that the issue only happens when credentials: "include"
is set. Not sure what I am missing here. Appreciate any help on this.