Typescript 管理员Sdk自定义声明返回null

Typescript 管理员Sdk自定义声明返回null,typescript,firebase,firebase-authentication,google-cloud-functions,firebase-admin,Typescript,Firebase,Firebase Authentication,Google Cloud Functions,Firebase Admin,我正在将firebase云功能与android一起使用创建具有自定义声明的用户,我已将自定义声明用作文档,但自定义声明=null:( 提前谢谢 代码: import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; const serviceAccount = require('../serviceAccountKey.json'); admin.initializeApp(

我正在将firebase云功能与android一起使用创建具有自定义声明的用户,我已将自定义声明用作文档,但自定义声明=null:(

提前谢谢

代码:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

const serviceAccount = require('../serviceAccountKey.json');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

exports.createSellerAccount = functions.https.onCall((data, context) => {
    const userEmail = data.email;
    const userPassword = data.password;

    return admin.auth().createUser({
        email: userEmail,
        password: userPassword
    }).then((userRecord) => {
        // See the UserRecord reference doc for the contents of userRecord.
        const additionalClaims = {
            premiumAccount: true
        };

        admin.auth().createCustomToken(userRecord.uid, additionalClaims)
            .then(function (customToken) {
                // Send token back to client
            })
            .catch(function (error) {
                console.log("Error creating custom token:", error);
            });

        return {
            sellerAccount: userRecord
        }
    }).catch((error) => {
        // console.log("Error creating new user:", error);
        if (error.code === "auth/email-already-exists") {
            throw new functions.https.HttpsError('already-exists', error.message);
        } else if (error.code === 'auth/invalid-email') {
            throw new functions.https.HttpsError('invalid-argument', error.message);
        } else {
            throw new functions.https.HttpsError('unknown', error.message);
        }
    });

})

您的代码创建了一个自定义令牌,而不是向现有令牌添加声明。要执行后一个操作,请参阅包含以下示例的内容:

因此,只需获取新用户的UID并使用它调用
setCustomUserClaims
,然后等待它传播到客户端,或者将令牌记录在节点脚本中,以确保声明在其中

请注意,我强烈建议您继续尝试隔离您的问题。不要在Android代码中显示令牌,而是将其记录在您的云函数代码中。更好的是,完全排除云函数,只运行一个local Node.js脚本

// Set admin privilege on the user corresponding to uid.

admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
});