【发布时间】: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