用户已经使用在Node中创建的自定义令牌通过Firebase身份验证。我想以类似的方式登录到另一个项目中的第二个数据库,使用管理员SDK。
我找不到任何关于如何专门为第二个数据库创建自定义令牌的信息。不管我使用哪个令牌进行身份验证(在前端),我当然会收到一个“auth/Cust-token-mismatch”火力基地错误,消息是“自定义令牌对应于不同的受众”在我看来,下面的“admin. auth().createCustomToken”似乎只为第一个应用程序创建它,第二次也为第一个应用程序创建它。
我找不到任何关于在哪里指定创建令牌的应用程序的信息,例如admin. auth("辅助").createCustomToken…或类似的东西。下面是有问题的代码。提前感谢您的时间。
//连接到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);
//GET SECOND TOKEN-这就是我需要帮助的地方
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);
您的admin
变量是使用您的第一个项目的凭据初始化的。因此从此admin
实例创建的令牌仅适用于该第一个项目。
要为另一个项目创建令牌,您需要创建另一个FirebaseApp
变量,该变量使用该项目的凭据进行初始化。
有关这方面的更多信息,还请参阅Firebase留档关于使用管理SDK初始化多个应用,其中包含以下方便示例:
// 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();