【问题标题】:Firebase: Create second Custom Token for "secondary" appFirebase:为“辅助”应用创建第二个自定义令牌
【发布时间】:2020-11-12 13:17:54
【问题描述】:

用户已使用在 Node.js 中创建的自定义令牌通过 Firebase 进行身份验证。我想使用 admin SDK 以类似的方式登录到另一个项目中的第二个数据库。

我找不到有关如何专门为第二个数据库创建自定义令牌的任何信息。无论我使用哪个令牌(在前端)进行身份验证,我当然都会收到“auth/custom-token-mismatch”firebase 错误,并显示消息“自定义令牌对应于不同的受众”。在我看来,下面的“admin.auth().createCustomToken”似乎只为第一个应用程序创建它,而第二次它也是为第一个应用程序创建它。

我找不到任何关于我将在哪里指定为其创建令牌的应用程序的信息,例如 admin.auth("secondary").createCustomToken.. 或类似的东西。下面是有问题的代码。提前感谢您的宝贵时间。

  • V

// 连接到 Firebase 项目 1

var firebaseConfig = {
    databaseURL: 'https://app1.firebaseio.com',
    apiKey: 'key1'
};

var credentials = admin.credential.cert({
    "type": "service_account",
    "project_id": "project_1_id",
    "private_key_id": "private_key_id_1",
    "private_key": "PRIVATE KEY 1",
    "client_email": "...",
    "client_id": "...",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..."
});

try {
    admin.initializeApp({
        credential: credentials,
        databaseURL: firebaseConfig.databaseURL
    });
} catch(err) {};

// 连接到 Firebase 项目 2

var firebaseConfig2 = {
    apiKey: "apikey",
    authDomain: "...firebaseapp.com",
    databaseURL: "...firebaseio.com",
    projectId: "project_2_id",
    storageBucket: "...appspot.com",
    messagingSenderId: "...",
    appId: "..."
};



var firebaseConfig2Credentials = {
    "type": "service_account",
    "project_id": "...",
    "private_key_id": "...",
    "private_key": "RPIVATE KEY 2",
    "client_email": "...gserviceaccount.com",
    "client_id": "...",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..."
};


try {
    admin.initializeApp({
        credential: firebaseConfig2Credentials,
        databaseURL: 'https://...firebaseio.com'
    }, "secondary");
} catch(err) {};

// 获取第一个令牌

var get_firebase_token = async (o) => new Promise((resolve, reject) => {
    admin.auth().createCustomToken(modules.btoa(o.email), {
        expiresAt: Date.now() + (1000 * 60 * 60 * 24 * 30),
        premiumAccount: true
    }).then(function(token) {
        resolve(token);
    }).catch(function(err) {
        resolve(null);
    });
});

firebaseConfig.token = await get_firebase_token(req.cookies);

// 获得第二个令牌 - 这就是我需要帮助的地方

var get_firebase_token2 = async (o) => new Promise((resolve, reject) => {
    admin.auth().createCustomToken(modules.btoa(o.email), {
        expiresAt: Date.now() + (1000 * 60 * 60 * 24 * 30),
        premiumAccount: true
    }).then(function(token) {
        resolve(token);
    }).catch(function(err) {
        resolve(null);
    });
});

firebaseConfig2.token = await get_firebase_token2(req.cookies);

【问题讨论】:

    标签: javascript node.js firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    您的 admin 变量已使用您的第一个项目的凭据进行初始化。因此,从此admin 实例中生成的令牌将仅适用于第一个项目。

    要为另一个项目创建令牌,您需要创建另一个 FirebaseApp 变量,该变量使用 那个 项目的凭据进行初始化。

    有关这方面的更多信息,另请参阅 initializing multiple apps with the Admin SDK 上的 Firebase 文档,其中包含这个方便的示例:

    // Initialize the default app
    admin.initializeApp(defaultAppConfig);
    
    // Initialize another app with a different config
    var otherApp = admin.initializeApp(otherAppConfig, 'other');
    
    console.log(admin.app().name);  // '[DEFAULT]'
    console.log(otherApp.name);     // 'other'
    
    // Use the shorthand notation to retrieve the default app's services
    var defaultAuth = admin.auth();
    var defaultDatabase = admin.database();
    
    // Use the otherApp variable to retrieve the other app's services
    var otherAuth = otherApp.auth();
    var otherDatabase = otherApp.database();
    

    【讨论】:

    • 首先,很抱歉我还不能投票(我还差 2 分),但我要感谢您抽出宝贵的时间。我将第二个应用程序初始化更改为: var secondary = admin.initializeApp(firebaseConfig2Credentials, 'secondary'); & var get_firebase_token2 = async (o) => new Promise((resolve, reject) => { secondary.auth().createCustomToken(modules.btoa(o.email), { ..... 但我得到了一个500错误。我需要在Node中声明数据库吗?现在它们在前端被引用。再次感谢。
    • 关于您现在遇到的错误,我不会立即看到问题所在,所以我可能值得用MCVE 发布一个新问题。
    • 经过一番修改后,我意识到只需在代码前面声明的变量“secondary”上调用 .auth() 就会破坏它。我只能做admin.auth。在前端,我没有这个问题,我可以声明secondary并在其上调用.auth。
    猜你喜欢
    • 2017-01-01
    • 2021-11-14
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2019-10-25
    • 2021-11-17
    • 2012-04-10
    • 2019-04-24
    相关资源
    最近更新 更多