Firebase管理员 - 创建自定义令牌:没有与提供的标识符对应的用户记录

时间:2017-10-11 10:30:16

标签: firebase-admin

我正在使用firebase管理员因为我想在不同的服务器中验证我的用户。我有以下代码,但我收到错误

{ code: 'auth/user-not-found',
  message: 'There is no user record corresponding to the provided identifier.' } }

文档 https://firebase.google.com/docs/auth/admin/create-custom-tokens

    var uid = user_dict['uid'];
    admin.auth().createCustomToken(uid)
        .then(function (customToken) {
            admin.auth().updateUser(uid, {
                //email: user_dict['email'],
                emailVerified: true,
                displayName: user_dict['displayName'],
                photoURL: user_dict['photoURL']
            })
            .then(function(userRecord) {

                response = {
                    token:customToken
                };
                res.send(response);

            })
            .catch(function(error) {
                console.log("Error updating user:", error);
                res.statusCode = 401;
                res.send("Error al actualizar usuario")
            });


        })
        .catch(function (error) {
            console.log("Error creating custom token:", error);
            res.statusCode = 401;
            res.send("El usuario no existe")
        });

此代码适用于1个用户的奇怪部分,但其余部分则不然。为什么呢?

更新

我将代码更改为:

var uid = user_dict['uid'];

admin.auth()。createCustomToken(UID)     .then(function(customToken){

    admin.auth().getUser(uid)
        .then(function(userRecord) {
            // See the UserRecord reference doc for the contents of userRecord.
            console.log("Successfully fetched user data:", userRecord.toJSON());
            admin.auth().updateUser(uid, {
                email: user_dict['email'],
                emailVerified: true,
                displayName: user_dict['displayName'],
                photoURL: user_dict['photoURL']
            })
            .then(function(userRecord) {

                response = {
                    token:customToken
                };
                res.send(response);

            })
            .catch(function(error) {
                console.log("Error updating user:", error);
                res.statusCode = 401;
                res.send("Error al actualizar usuario")
            });
        })
        .catch(function(error) {
            console.log("Error fetching user data:", error);

            admin.auth().createUser({
                uid:uid,
                email: user_dict['email'],
                emailVerified: true,
                displayName: user_dict['displayName'],
                photoURL: user_dict['photoURL'],
                password:req.body['password']
            })
            .then(function(userRecord) {
                // See the UserRecord reference doc for the contents of userRecord.
                console.log("Successfully created new user:", userRecord.uid);
                response = {
                    token:customToken
                };
                res.send(response);
            })
            .catch(function(error) {
                console.log("Error creating new user:", error);
                res.statusCode = 401;
                res.send("Error al crear el usuario")
            });

        });

})
.catch(function (error) {
    console.log("Error creating custom token:", error);
    res.statusCode = 401;
    res.send("El usuario no existe")
});

您可以在下面的屏幕截图中看到carlos @ mail是使用createUser()函数创建的,另一封电子邮件是ricardo @ mail不是。他们没有相同的提供者为什么会有这样的差异?

enter image description here

1 个答案:

答案 0 :(得分:1)

createCustomToken()接受任意用户ID字符串。但updateUser()没有。传递给updateUser()的UID必须已存在于Firebase项目中。您需要致电createUser()首先在Firebase中创建这些用户帐户。