Firestore Cloud Function API Endpoint

huangapple go评论69阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月7日 14:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定