我有以下问题。我已经实现了Firebase云功能,该功能从Firestore返回用户个人资料。我的云功能要求您登录到Firebase才能查询它们。
现在,在我的应用中,我实现了在FirebaseAuth.instance.onAuthStateChanged
上调用用于获取用户个人资料的云函数的功能。但是,当我登录到应用程序时,触发的调用会返回原因为UNAUTHENTICATED的PlatformException,尽管在此之前的行中,我使用FirebaseAuth.instance.currentUser()
检查当前用户,并且获得了正确且有效的uid。 Firebase中没有涉及此功能的日志,仅适用于全部成功且也已在FirebaseAuth.instance.onAuthStateChanged
上调用的其他功能。
这是云功能,它实际上非常简单:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
export default functions.https.onCall(async (data, context) => {
console.info(`[api-userProfile] called with userUid=${data.userUid}`);
const user: FirebaseFirestore.QuerySnapshot = await admin
.firestore()
.collection("userProfiles")
.where("userUid", "==", data.userUid)
.get();
if (user.docs.length === 1) {
console.info(
`[api-userProfile] returning user for userUid=${data.userUid}`
);
return user.docs[0].data();
}
console.info(`[api-userProfile] could not find userUid=${data.userUid}`);
return null;
});
这就是我所说的:
final _disposeBag = CompositeSubscription();
final userProfile = BehaviorSubject<UserProfile>();
final _userProfilesFn =
CloudFunctions.instance.getHttpsCallable(functionName: "api-userProfile");
UserProfileStore() {
_disposeBag.add(FirebaseAuth.instance.onAuthStateChanged
.map((v) => v != null ? v : throw "")
.handleError((v) => userProfile.add(null))
.asyncMap((v) {
print(v.uid); // here the correct uid is printed
return _userProfilesFn.call({"userUid": v.uid});
})
.map((v) => v as Map)
.map((v) => UserProfile.fromJson(v))
.addTo(userProfile)
.listen((_) {}));
}
抛出的错误是(第一行是登录用户的uid):
I/flutter ( 9370): DXrcqtU34Dh8UfZYnsNdxn31hqx2
E/flutter ( 9370): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(functionsError, Cloud function failed with exception., {code: UNAUTHENTICATED, details: null, message: UNAUTHENTICATED})
E/flutter ( 9370): #0 StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:569
E/flutter ( 9370): #1 MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:321
E/flutter ( 9370): <asynchronous suspension>
E/flutter ( 9370): #2 MethodChannelCloudFunctions.callCloudFunction
package:cloud_functions_platform_interface/src/method_channel_cloud_functions.dart:43
E/flutter ( 9370): #3 HttpsCallable.call
package:cloud_functions/src/https_callable.dart:33
E/flutter ( 9370): #4 new UserProfileStore.<anonymous closure>
答案 0 :(得分:0)
我认为您应该使用命名导出,如下所示:
export const api-userProfile = functions.https.onCall(async (data, context) => {...});