我可以将 auth:import 和 auth:export 工具与 Firebase Auth 模拟器一起使用吗?
Can I use the auth:import and auth:export tools with the Firebase Auth Emulator?
我想在启动时使用我的测试用户帐户预加载 firebase auth 模拟器,就像我使用其 import/export 选项为 Firestore 模拟器做的一样。我尝试使用 auth:import 和 auth:export,而我的模拟器是 运行ning 但它连接到我们实际的开发 firebase 项目,而不是模拟器。 运行 auth:import 和 auth:export 有没有针对 auth 模拟器的?
作为参考,我指的是这些命令 (https://firebase.google.com/docs/cli/auth) and this emulator (https://firebase.google.com/docs/emulator-suite/connect_auth)。
自从 firebase 确认这不受支持后,我改用云函数来执行此操作。我首先将我希望在启动时加载的用户导出为 json 格式,然后我 运行 每次重新启动模拟器时都会使用此云功能:
exports.populateAuthUsers = functions.https.onRequest(async (req, res) => {
try {
admin.initializeApp();
const users = [
{ uid: "user1", email: "user1@test.com", password: "password" },
// put all test users you want populated here
];
const results = [];
for (let user of users) {
const result = await admin.auth().createUser(user);
results.push(result);
}
return res.status(200).send(results);
} catch (error) {
console.error('Error: ', error);
return res.status(500).send(error);
}
});
即使有几百个用户,运行 也只需要几秒钟。请确保永远不要将它部署到任何实际的 firebase 环境中!仅限本地使用。
谢谢@alex-egli 这很有帮助
这是我的版本,在您的版本基础上做了 3 处小改动
避免 eslint 警告https://eslint.org/docs/rules/no-await-in-loop
(我不认为你的代码在这里不好,警告可以被静音)
添加一项检查,确认这是 运行 在模拟器上
已添加用户显示名称
exports.populateAuthUsers = functions.https.onRequest(async (req, res) => {
if (!process.env["FUNCTIONS_EMULATOR"]) {
return res
.status(403)
.send("ACCESS DENIED. This function is ONLY available via an emulator");
}
const users = [
{
uid: "user1",
displayName: "one Local Admin",
email: "one@test.com",
password: "password",
},
{
uid: "user2",
displayName: "two Local Admin",
email: "two@test.com",
password: "password",
},
// put all test users you want populated here
];
const results = [];
const promises = [];
for (let user of users) {
let promise = admin
.auth()
.createUser(user)
.then((result) => {
return result;
})
.catch((error) => {
return error.message; // continue on errors (eg duplicate users)
});
promises.push(promise);
}
await Promise.all(promises).then((result) => {
results.push(result);
return;
});
res.header("Content-type", "application/json");
return res.status(200).send(JSON.stringify(results));
});
执行此操作的功能现已添加到 Firebase 工具中
旧的答案仍然有效并且可能有用
https://github.com/firebase/firebase-tools/releases/tag/v9.1.0
Support emulators:export and import for Auth Emulator (#2955).
firebase help auth:import
Usage: firebase auth:import [options] [dataFile]
import users into your Firebase project from a data file(.csv or .json)
Options:
--hash-algo <hashAlgo> specify the hash algorithm used in password for these accounts
--hash-key <hashKey> specify the key used in hash algorithm
--salt-separator <saltSeparator> specify the salt separator which will be appended to salt when verifying password. only used by SCRYPT now.
--rounds <rounds> specify how many rounds for hash calculation.
--mem-cost <memCost> specify the memory cost for firebase scrypt, or cpu/memory cost for standard scrypt
--parallelization <parallelization> specify the parallelization for standard scrypt.
--block-size <blockSize> specify the block size (normally is 8) for standard scrypt.
--dk-len <dkLen> specify derived key length for standard scrypt.
--hash-input-order <hashInputOrder> specify the order of password and salt. Possible values are SALT_FIRST and PASSWORD_FIRST. MD5, SHA1, SHA256, SHA512, HMAC_MD5, HMAC_SHA1, HMAC_SHA256, HMAC_SHA512 support this flag.
-h, --help output usage information
admin
.auth()
.importUsers(
[
{
uid: 'some-uid',
email: 'user@example.com',
// Must be provided in a byte buffer.
passwordHash: Buffer.from('password-hash'),
// Must be provided in a byte buffer.
passwordSalt: Buffer.from('salt'),
},
],
{
hash: {
algorithm: 'STANDARD_SCRYPT',
memoryCost: 1024,
parallelization: 16,
blockSize: 8,
derivedKeyLength: 64,
},
}
)
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
完成此操作的最简单方法是在 运行 模拟器时传入 --export-on-exit
,例如:firebase emulators:start --export-on-exit .firebase-emulators
,这会将所有测试数据转储到退出模拟器时的指定目录。
然后您可以根据需要将其检入您的源代码(确保您的模拟器中没有敏感数据),并在每次启动时执行 firebase emulators:start --import .firebase-emulators
读取它。
我想在启动时使用我的测试用户帐户预加载 firebase auth 模拟器,就像我使用其 import/export 选项为 Firestore 模拟器做的一样。我尝试使用 auth:import 和 auth:export,而我的模拟器是 运行ning 但它连接到我们实际的开发 firebase 项目,而不是模拟器。 运行 auth:import 和 auth:export 有没有针对 auth 模拟器的?
作为参考,我指的是这些命令 (https://firebase.google.com/docs/cli/auth) and this emulator (https://firebase.google.com/docs/emulator-suite/connect_auth)。
自从 firebase 确认这不受支持后,我改用云函数来执行此操作。我首先将我希望在启动时加载的用户导出为 json 格式,然后我 运行 每次重新启动模拟器时都会使用此云功能:
exports.populateAuthUsers = functions.https.onRequest(async (req, res) => {
try {
admin.initializeApp();
const users = [
{ uid: "user1", email: "user1@test.com", password: "password" },
// put all test users you want populated here
];
const results = [];
for (let user of users) {
const result = await admin.auth().createUser(user);
results.push(result);
}
return res.status(200).send(results);
} catch (error) {
console.error('Error: ', error);
return res.status(500).send(error);
}
});
即使有几百个用户,运行 也只需要几秒钟。请确保永远不要将它部署到任何实际的 firebase 环境中!仅限本地使用。
谢谢@alex-egli 这很有帮助
这是我的版本,在您的版本基础上做了 3 处小改动
避免 eslint 警告https://eslint.org/docs/rules/no-await-in-loop
(我不认为你的代码在这里不好,警告可以被静音)添加一项检查,确认这是 运行 在模拟器上
已添加用户显示名称
exports.populateAuthUsers = functions.https.onRequest(async (req, res) => {
if (!process.env["FUNCTIONS_EMULATOR"]) {
return res
.status(403)
.send("ACCESS DENIED. This function is ONLY available via an emulator");
}
const users = [
{
uid: "user1",
displayName: "one Local Admin",
email: "one@test.com",
password: "password",
},
{
uid: "user2",
displayName: "two Local Admin",
email: "two@test.com",
password: "password",
},
// put all test users you want populated here
];
const results = [];
const promises = [];
for (let user of users) {
let promise = admin
.auth()
.createUser(user)
.then((result) => {
return result;
})
.catch((error) => {
return error.message; // continue on errors (eg duplicate users)
});
promises.push(promise);
}
await Promise.all(promises).then((result) => {
results.push(result);
return;
});
res.header("Content-type", "application/json");
return res.status(200).send(JSON.stringify(results));
});
执行此操作的功能现已添加到 Firebase 工具中
旧的答案仍然有效并且可能有用
https://github.com/firebase/firebase-tools/releases/tag/v9.1.0
Support emulators:export and import for Auth Emulator (#2955).
firebase help auth:import
Usage: firebase auth:import [options] [dataFile]
import users into your Firebase project from a data file(.csv or .json)
Options:
--hash-algo <hashAlgo> specify the hash algorithm used in password for these accounts
--hash-key <hashKey> specify the key used in hash algorithm
--salt-separator <saltSeparator> specify the salt separator which will be appended to salt when verifying password. only used by SCRYPT now.
--rounds <rounds> specify how many rounds for hash calculation.
--mem-cost <memCost> specify the memory cost for firebase scrypt, or cpu/memory cost for standard scrypt
--parallelization <parallelization> specify the parallelization for standard scrypt.
--block-size <blockSize> specify the block size (normally is 8) for standard scrypt.
--dk-len <dkLen> specify derived key length for standard scrypt.
--hash-input-order <hashInputOrder> specify the order of password and salt. Possible values are SALT_FIRST and PASSWORD_FIRST. MD5, SHA1, SHA256, SHA512, HMAC_MD5, HMAC_SHA1, HMAC_SHA256, HMAC_SHA512 support this flag.
-h, --help output usage information
admin
.auth()
.importUsers(
[
{
uid: 'some-uid',
email: 'user@example.com',
// Must be provided in a byte buffer.
passwordHash: Buffer.from('password-hash'),
// Must be provided in a byte buffer.
passwordSalt: Buffer.from('salt'),
},
],
{
hash: {
algorithm: 'STANDARD_SCRYPT',
memoryCost: 1024,
parallelization: 16,
blockSize: 8,
derivedKeyLength: 64,
},
}
)
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
完成此操作的最简单方法是在 运行 模拟器时传入 --export-on-exit
,例如:firebase emulators:start --export-on-exit .firebase-emulators
,这会将所有测试数据转储到退出模拟器时的指定目录。
然后您可以根据需要将其检入您的源代码(确保您的模拟器中没有敏感数据),并在每次启动时执行 firebase emulators:start --import .firebase-emulators
读取它。