firebase - Flutter 调用 firebase 云函数 admin.auth.updateUser

标签 firebase flutter google-cloud-firestore google-cloud-functions busboy

编辑**

好的,由于提供的第一个答案,我能够使参数正常工作,但现在我遇到了一个问题,即我的函数完全在 Firebase 中创建一个新用户,而不是更新现有用户,即我传递给auth.admin.updateUser 是我要更新电子邮件的现有用户的 uid。这是更新的云功能,它添加了一个新用户而不是更新现有用户:

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

  const email = data.email;
  const uid = data.uid;

  admin.auth().updateUser(uid, {
              email: email
          })
          .then(function(userRecord) {
              // See the UserRecord reference doc for the contents of userRecord.
              console.log("Successfully updated user", userRecord.toJSON());
              return response.status(200).json(userRecord.toJSON());
          })
          .catch(function(error) {
              console.log("Error updating user:", error);
              return response.status(404).json({
                  error: 'Something went wrong.'
              });
          });
});

我从 firebase 文档中获得了该功能,但它没有按照我的预期进行。

原帖**

在我的 flutter 代码中调用云函数时,我遇到了一些困难。我遇到的问题是 uid 和 email 字段未定义,即使我使用 busboy 字段将它们传递给云函数也是如此。

我正在尝试将电子邮件和 uid 字段传递给函数,如下所示:

final request = http.MultipartRequest('POST', Uri.parse('****************my function url************'));

request.fields['email'] = Uri.encodeComponent(newEmail);
request.fields['uid'] = Uri.encodeComponent(selectedUser.uid);

request.headers['Authorization'] = 'Bearer ${_authenticatedUser.token}';

  final http.StreamedResponse streamedResponse = await request.send();

在 Node.js 方面,我尝试使用 busboy 使用这些字段,这是我在 Node.js 中的云函数:

exports.changeEmail = functions.https.onRequest((request, response) => {

if (!request.headers.authorization ||
    !request.headers.authorization.startsWith('Bearer ')
) {
    return response.status(401).json({
        error: 'Unauthorized.'
    });
}

let idToken;
idToken = request.headers.authorization.split('Bearer ')[1];
let email;
let uid;

const busboy = new Busboy({
    headers: request.headers
});

busboy.on('field', (fieldname, value) => {

    if (fieldname == 'email') {


        email = decodeURIComponent(value);
    }

    if (fieldname == 'uid') {

        uid = decodeURIComponent(value);
    }
});



admin.auth().updateUser(uid, {
        email: email
    })
    .then(function(userRecord) {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Successfully updated user", userRecord.toJSON());
        return response.status(200).json(userRecord.toJSON());
    })
    .catch(function(error) {
        console.log("Error updating user:", error);
        return response.status(404).json({
            error: 'Something went wrong.'
        });
    });

});

即使我通过 busboy 字段传递字段,它们也没有在函数中设置,我在这里做错了什么吗?

最佳答案

你为什么不使用 callable function ?它将自动接收身份验证数据。

文档甚至有关于如何获取 uid 和电子邮件的示例:

声明函数:

exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

从上下文参数中获取用户属性:

// Message text passed from the client.
const text = data.text;
// Authentication / user information is automatically added to the request.
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;

从您的 Flutter 代码中调用函数:

安装 cloud_functions打包然后:

import 'package:cloud_functions/cloud_functions.dart';

await CloudFunctions.instance.call(functionName: 'addMessage');

如果用户在调用函数之前已通过身份验证,这就是您需要做的所有事情。

您还可以向函数传递额外的参数:

await CloudFunctions.instance.call(functionName: 'addMessage', parameters: {"email": "whatever@example.com"});

任何参数都将传递给函数端的数据参数。

关于firebase - Flutter 调用 firebase 云函数 admin.auth.updateUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55051419/

相关文章:

javascript - Firebase 存储 getDownloadURL() 不返回工作 url

flutter - '_Type' 不是类型 'Widget' 的子类型

Flutter 后退按钮隐藏在导航中

android - 从 Firestore 中检索 null 对象,当它实际上不是 null

firebase - 离线超过 30 分钟时,Firestore 是否向我们收费?

firebase - 共享 Firestore 文档或将协作者添加到 Firestore 文档

node.js - 删除磁盘后使用 npm 安装 firebase 时出错

node.js - 可能不存在的字段上的“orderBy”会完全跳过文档

node.js - 使用 multer 将图像上传到 Firebase 存储问题

Flutter setState 不更新小部件