Problem The getPotentialMatches Cloud Function in my Firebase project fails with the error User must be authenticated because context.auth is consistently undefined. The client sends a valid token, but the server fails to validate it, suggesting a Firebase Functions runtime or configuration issue.
What We Did Initial Diagnosis: Client sends a valid token, but context.auth is undefined on the server. Client Call: dart
final result = await FirebaseFunctions.instance.httpsCallable("getPotentialMatches").call({"preferences": {}});
Server Code (Original):
javascript
exports.getPotentialMatches = functions.https.onCall(async (data, context) => {
if (!context.auth) throw new functions.https.HttpsError("unauthenticated", "User must be authenticated");
const userId = context.auth.uid;
// Logic to fetch matches
});
UID-Based Authentication: Sent uid in the payload (data.uid): dart
final result = await FirebaseFunctions.instance.httpsCallable("getPotentialMatches").call({"uid": user.uid, "preferences": {}});
javascript
exports.getPotentialMatches = functions.https.onCall(async (data) => {
const uid = data.uid;
if (!uid || typeof uid !== "string") throw new functions.https.HttpsError("invalid-argument", "UID must be a non-empty string");
await admin.auth().getUser(uid);
// Logic to fetch matches
});
Failed with UID must be a non-empty string.
Permissions: Granted Firebase Authentication Viewer and other roles to service accounts, ensuring token validation permissions.
Issue Persistence Despite these efforts—moving functions, reverting to context.auth.uid, granting permissions, updating SDKs, and simplifying logging—the issue persists: context.auth is undefined, causing User must be authenticated. This suggests a deeper runtime or configuration issue.