Context.auth 是未定义的 Firestore 和 Firebase 函数

问题描述 投票:0回答:1

您好,我正在尝试访问拨打此电话的用户的 auth.id。这是我与 Stripe 集成的一部分。

我正在使用 Firestore,并且我发现 Firestore 应用程序不支持传递上下文?现在还是这样吗?很多讨论似乎已经有 5 年左右的历史了。

我还如何将此信息传递给函数?

const { onCall } = require('firebase-functions/v2/https');
const { error } = require('firebase-functions/logger');
import * as Postmark from 'postmark';
import { v4 as uuidv4 } from 'uuid';
import admin = require('firebase-admin');
import { FieldValue } from 'firebase-admin/firestore';
import * as functions from 'firebase-functions';
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
import * as dotenv from 'dotenv';
dotenv.config();

admin.initializeApp();

...

exports.createVerificationSession = onCall(async (data: any, context: any) => {
  console.log('context');
  console.log(context);
  console.log('Creating verification session');
  if (!context.auth) {
    console.log('No authentication context available.');
    throw new functions.https.HttpsError(
      'unauthenticated',
      'The function must be called while authenticated.',
    );
  }
  try {
    const verificationSession =
      await stripe.identity.verificationSessions.create({
        type: 'document',
        metadata: {
          user_id: context.auth.uid,
        },
      });
    const clientSecret = verificationSession.client_secret;
    console.log('Client secret:', clientSecret);
    return { clientSecret };
  } catch (error) {
    console.error('Error creating verification session:', error);
    throw new functions.https.HttpsError(
      'internal',
      'Unable to create verification session.',
    );
  }
});

这是我的 FE 代码:

function Payment() {
  const [clientSecret, setClientSecret] = useState('');

  const fetchClientSecret = async () => {
    console.log('Fetching client secret');
    const functions = getFunctions();
    const createVerificationSession = httpsCallable(
      functions,
      'createVerificationSession',
    );

    try {
      console.log('Calling createVerificationSession');
      await createVerificationSession().then((result) => {
        console.log('Result:', result);
      });
      // setClientSecret(response.data.clientSecret);
    } catch (error) {
      console.error('Failed to fetch client secret:', error);
      // Handle errors here appropriately
    }
  };

  useEffect(() => {
    fetchClientSecret();
  }, []);

  // Ensure stripePromise and clientSecret are loaded before rendering the Elements provider
  if (!stripePromise || !clientSecret) {
    return <p>Loading payment details...</p>;
  }

  const options = {
    clientSecret: clientSecret,
  };

  return (
    <Elements stripe={stripePromise} options={options}>
      <CheckoutForm />
    </Elements>
  );
}

我可以从那里过去吗?

auth.uid
还是什么?

firebase google-cloud-firestore google-cloud-functions stripe-payments
1个回答
0
投票

你只是在这里犯了一个小错误

exports.createVerificationSession = functions.https.onCall(async (data:any, context:any) => {
//<-- you forgot to add `functions.https` here onCall(async (data: any, context: any) => {
© www.soinside.com 2019 - 2024. All rights reserved.