我在 Firebase 函数未验证时收到错误

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

我有一个 flutter 应用程序,现在我需要在我的应用程序中接受付款,并且我想检查付款是否是通过使用 firebase 函数进行的,但我不断收到未经验证的错误。

我添加了应用程序检查,因为这是实现我想要的支付功能所必需的。我在 appcheck 中发现我的请求已得到验证,所以我认为这不是问题所在。但如果我调用该函数,我会收到错误 [firebase_functions/unauthenticated} UNAUTHENTICATED。我已经更改了整个函数只是为了测试,但我仍然遇到相同的错误:

索引.js

const admin = require("firebase-admin");

admin.initializeApp();

exports.testFunction = functions.https.onCall((data, context) => {
  console.log("Auth context:", context.auth);
  console.log("App Check token:", context.app);

  // Check if the user is authenticated
  if (!context.auth) {
    console.error("User is not authenticated.");
    throw new functions.https.HttpsError(
        "failed-precondition",
        "The function must be called while authenticated.",
    );
  }

  // Check if App Check is enabled and working
  if (!context.app) {
    console.error("App Check token is missing or invalid.");
    throw new functions.https.HttpsError(
        "failed-precondition",
        "App Check is not verified or missing.",
    );
  }

  // Return a simple success message
  return {message: "Authentication and App Check are working!"};
});

这是我测试 firebase 功能的颤动屏幕:

import 'package:flutter/material.dart';
import 'package:cloud_functions/cloud_functions.dart';
import 'package:firebase_auth/firebase_auth.dart';

class TestFunctionScreen extends StatefulWidget {
  @override
  _TestFunctionScreenState createState() => _TestFunctionScreenState();
}

class _TestFunctionScreenState extends State<TestFunctionScreen> {
  String _result = "Press the button to test Firebase Function";

  Future<void> testFunction() async {
    try {
      // Ensure the user is logged in
      User? user = FirebaseAuth.instance.currentUser;
      if (user == null) {
        setState(() {
          _result = "User not logged in. Please log in and try again.";
        });
        return;
      }
      print("Authenticated user: ${user.uid}");

      // Call Firebase Function
      final HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('testFunction');
      final response = await callable.call();
      setState(() {
        _result = "Function Response: ${response.data}";
      });
    } catch (e, stackTrace) {
      print("Error calling function: $e");
      print(stackTrace);
      setState(() {
        _result = "Error: $e";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Test Firebase Function"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              _result,
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: testFunction,
              child: Text("Test Firebase Function"),
            ),
          ],
        ),
      ),
    );
  }
}

这是我启动应用程序时的日志,直到我按下测试 firebase 功能按钮。

I/flutter (12954): App Check Token: eyJraWQiOiJRNmZ5eEEiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxOjQyMDMzMzQ2Mzk0MDphbmRyb2lkOmJlZDJmYjBiODkjE3MzE5NDQ4NDcsImp0aSI6InlwR0o5Q2g2YndpRDllbnNNN3VhYzVZQ0JtWDFXVWVuR1dQUkVIQW5YZzgifQ.VVQkV5d8GzU0KMMXe-JLE0Zkrg3809HCIbBOEf0oqJE7JFRyN6xJYMuOzez9Y1YR-aVbUtVUO7qcMg122znB9LFyd1kYZD-nx1N4umICG83DL12SUI9NWVYYv64L5qtbOZsP3eYW9ytqH8stHKka0bfe1y_eB2v7RzgNeSSpBuylRQHDJHkI2FEWr69PnE8WP6nD7K4cEZaQBR5VZPHH51aWkoj-VlODEdJBqMQXM6CdbQ1ZHDEjZvOucLUQlcbq11dsr4wvnO68SaILiq_rapIzvZCo7FltLhYJzGzjl4BBz6wM6YSQzbhA198_L0PS46tWFJ8MIPCedqWe434EAh_mX8FJcGlA5PdrdHJkxCcFB6oPbjViBldbsrSlEjNbQX1OFeBShv5PAumbj2EYbPt_1ensR0hvkaNw2oNwPS_4TyavizRhN_Vcg
I/flutter (12954): Authenticated user: 6hxNET0B1cbZ9hLMVk6p13
W/s.homes_captain(12954): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (unsupported,core-platform-api, reflection, allowed)
W/stain(12954): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (unsupported,core-platform-api, reflection, allowed)
W/s.tain(12954): Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (unsupported,core-platform-api, reflection, allowed)
I/flutter (12954): Error calling function: [firebase_functions/unauthenticated] UNAUTHENTICATED
I/flutter (12954): 
I/flutter (12954): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
I/flutter (12954): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
I/flutter (12954): <asynchronous suspension>
I/flutter (12954): #2      MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:22:24)
I/flutter (12954): <asynchronous suspension>
I/flutter (12954): #3      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:49:37)
I/flutter (12954): <asynchronous suspension>
I/flutter (12954): #4      _TestFunctionScreenState.testFunction (package:homes_captain/paymentscreen.dart:27:24)
I/flutter (12954): <asynchronous suspension>
I/flutter (12954): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
I/flutter (12954): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
I/flutter (12954): <asynchronous suspension>
I/flutter (12954): #2      MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:22:24)
I/flutter (12954): <asynchronous suspension>
I/flutter (12954): #3      HttpsCallable.call (package:cloud_functions/src/https_callable.dart:49:37)
I/flutter (12954): <asynchronous suspension>
I/flutter (12954): #4      _TestFunctionScreenState.testFunction (package:homes_captain/paymentscreen.dart:27:24)
I/flutter (12954): <asynchronous suspension>
android flutter firebase-authentication google-cloud-functions firebase-app-check
1个回答
0
投票

您需要在客户端启用应用程序检查 - 或在云功能端禁用它。

更改:

exports.testFunction = functions.https.onCall((data, context) => {

致:

exports.testFunction = functions.https.onCall({ enforceAppCheck: false }, async (request) => {

或者传递类似的东西作为

.getHttpsCallable(name, options)
的第二个参数:

HttpsCallableOptions.Builder().setLimitedUseAppCheckTokens(true).build()

HttpsCallableOptions
Kotlin 中最少存在,不确定 Dart 语法。

© www.soinside.com 2019 - 2024. All rights reserved.