【问题标题】:Flutter calling firebase cloud function admin.auth.updateUserFlutter 调用 firebase 云函数 admin.auth.updateUser
【发布时间】:2019-07-29 18:57:25
【问题描述】:

编辑**

好的,由于提供的第一个答案,我能够使参数正常工作,但现在我遇到了一个问题,即我的函数完全在 Firebase 中创建一个新用户,而不是更新现有用户,即我传入的 uid 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 文档中获得了这个功能,但它并没有按照我的预期去做。

原帖**

从我的颤振代码中调用云函数时,我在让云函数工作时遇到了一些困难。我遇到的问题是 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 字段一起传递,但它们没有在函数中设置,我在这里做错了吗?

【问题讨论】:

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


    【解决方案1】:

    为什么不使用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"});
    

    任何参数都会传给函数端的data参数。

    【讨论】:

    • 非常感谢您的完美工作!! iOS 没有在云功能包中正确设置区域存在一个小问题,但在修复后,我成功地以管理员身份在我的应用程序中更新了用户电子邮件!我给了你一票,但我没有足够的声望来展示它!
    • 但是现在我的功能本身存在问题,我想更新现有用户的电子邮件,但似乎完全是在添加新用户
    • admin.auth().updateUser(uid, { email: email }) .then(function(userRecord) { // 有关 userRecord.console.log(" 的内容,请参阅 UserRecord 参考文档成功更新用户", userRecord.toJSON()); return response.status(200).json(userRecord.toJSON()); }) .catch(function(error) { console.log("更新用户出错:", error); return response.status(404).json({ error: '出了点问题。' }); });
    • 很高兴它成功了!但它是否创建了一个具有不同 uid 的新用户?也许如果您针对此特定问题和详细信息创建一个新问题,那么人们会更容易为您提供帮助。
    • @RicardoSmania 我看到您可以将键:值对作为parameters: {"email": "whatever@example.com"} 传递,因此在函数内部它将以data.emaildata 的形式出现,需要进行json 解码然后获取email 参数来自它?
    猜你喜欢
    • 2021-08-29
    • 2019-01-18
    • 2021-09-13
    • 2023-03-08
    • 2018-05-17
    • 1970-01-01
    • 2020-01-04
    • 2020-01-28
    • 2018-10-02
    相关资源
    最近更新 更多