英文:
Firestore Cloud Function API Endpoint
问题
I want anyone (unauthenticated users) to be able to call an API and return the tier of plan they are on depending on their FN, LN, DOB. I created an HTTP endpoint via a Cloud Function but receive a 403 error even after updating permissions.
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
export const checkEligibility = functions.https.onRequest(async (req, res) => {
const firstName = req.body.firstName;
const lastName = req.body.lastName;
const dateOfBirth = new Date(req.body.dateOfBirth);
console.log(firstName);
const eligibleUsersRef = admin.firestore().collection("eligible_people");
const querySnapshot = await eligibleUsersRef
.where("firstName", "==", firstName)
.where("lastName", "==", lastName)
.where("dob", "==", admin.firestore.Timestamp.fromDate(dateOfBirth))
.get();
if (querySnapshot.empty) {
// User is not eligible, return an error message
res.status(403).send("User is not eligible");
} else {
// User is eligible, return the name of the tier
const eligibleUser = querySnapshot.docs[0].data();
res.json({tier: eligibleUser.tier});
}
});
When I call this function from an app, I receive the following log:
Function execution took 4077 ms, finished with status code: 403
I've updated the permissions of this in the Google Cloud so that all users are invokers, similar to this: https://stackoverflow.com/questions/60237167/firebase-functions-https-403-forbidden. However, I'm still getting this 403 error—what am I doing wrong?
英文:
I want anyone (unauthenticated users) to be able to call an API an return the tier of plan they are on depending on their FN, LN, DOB. I created an HTTP endpoint via a Cloud Function but receive a 403 error even after updating permissions.
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
export const checkEligibility = functions.https.onRequest(async (req, res) => {
const firstName = req.body.firstName;
const lastName = req.body.lastName;
const dateOfBirth = new Date(req.body.dateOfBirth);
console.log(firstName);
const eligibleUsersRef = admin.firestore().collection("eligible_people");
const querySnapshot = await eligibleUsersRef
.where("firstName", "==", firstName)
.where("lastName", "==", lastName)
.where("dob", "==", admin.firestore.Timestamp.fromDate(dateOfBirth))
.get();
if (querySnapshot.empty) {
// User is not eligible, return an error message
res.status(403).send("User is not eligible");
} else {
// User is eligible, return the name of the tier
const eligibleUser = querySnapshot.docs[0].data();
res.json({tier: eligibleUser.tier});
}
});
When I call this function from an app, I receive the following log:
Function execution took 4077 ms, finished with status code: 403
I've updated the permissions of this in the Google Cloud so that all users are invokers, similar to this: https://stackoverflow.com/questions/60237167/firebase-functions-https-403-forbidden. However, I'm still getting this 403 error--what am I doing wrong?
答案1
得分: 1
Timestamp.fromDate()
方法的结果与数据库中存储的时间戳不同。其后果是您的querySnapshot
始终为空。
由于您选择的是日期(而不是日期/时间),我建议使用一个简单的字符串格式,例如YYYYMMDD
。
英文:
The result of the Timestamp.fromDate()
method is different from the time Timestamp stored in the database. The consequence is that your querySnapshot
is always empty.
Since you select on a date (and not a date/time) I advise to use a simple string formatted for example as YYYYMMDD
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论