1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Firebase Authentication にデータをインポート・エクスポートおよびデリートする方法

Posted at

はじめに

Firebase Authentication にデータをインポート・エクスポートおよびデリートする方法をまとめます。
Firebase Authentication にデータを出し入れするときがあるかと思います。
私の場合は、既存のインフラから新しいインフラに移行する際にデータを出し入れする必要がありました。

対象読者

・インフラ初級者
・Firebase Authentication に慣れていない方
Firebase Authentication にデータを出し入れする方法をサクッと知りたい方

参考

Firebase Authentication でのデータ移行の流れ

  1. sample_projectA からデータをエクスポートする
  2. sample_projectB のテストデータを削除する(データが空であればスキップ)
  3. 1 でエクスポートしたsample_projectA のデータを、sample_projectB にインポートする

1. sample_projectA からデータをエクスポートする

$ firebase auth:export file_name.json --project=sample_projectA

Exporting accounts to file_name.json
✔  Exported 500 account(s) successfully.

サービスアカウントの権限を確認してください。
権限が不足していると、パスワードを適切にエクスポートできない場合がありました。

2. sample_projectB のテストデータを削除する(データが空であればスキップ)

ここではスクリプトを作成して、Firebase Authentication のデータを削除します。

firebase_authentication_user_delete_all.js
const admin = require("firebase-admin");
const _ = require('lodash/array');
// 対象のFirebaseプロジェクトに対応するサービスアカウントファイルを同じディレクトリに配置
const serviceAccount = require("./serviceAccountKey.json");
const databaseURL = "https://sample_projectA.firebaseio.com"

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

const deleteAllUsers = (nextPageToken) => {
  admin
    .auth()
    .listUsers(1000, nextPageToken)
		.then((listUsersResult) => {
      const uids = listUsersResult.users.map(userRecord => {
        return userRecord.uid;
      });

			admin
				.auth()
				.deleteUsers(uids)
				.then((deleteUsersResult) => {
					console.log(`Successfully deleted ${deleteUsersResult.successCount} users`);
					console.log(`Failed to delete ${deleteUsersResult.failureCount} users`);
					deleteUsersResult.errors.forEach((err) => {
						console.log(err.error.toJSON());
					});
				})
				.catch((error) => {
					console.log('Error deleting users:', error);
				});
      if (listUsersResult.pageToken) {
        deleteAllUsers(listUsersResult.pageToken);
      }
    })
    .catch((error) => {
      console.log('Error listing users:', error);
    });
};

deleteAllUsers();
$ node firebase_authentication_user_delete_all.js

Successfully deleted 50 users
Failed to delete 0 users

3. 1 でエクスポートしたsample_projectA のデータを、sample_projectB にインポートする

スクリーンショット 2023-02-24 11.36.42.png

↑の3点リーダーを押下して、パタメーターを確認します。

スクリーンショット 2023-02-24 11.37.28.png

パスワードハッシュパラメーターは、エクスポート元のプロジェクトを参照してください。
今回の場合は、sample_projectA です。

firebase auth:import file_name.json \
    --hash-algo=your_algo \
    --hash-key=your_key \
    --salt-separator=your_salt \
    --rounds=number \
    --mem-cost=number \
    --project=sample_projectB

Processing file_name.json (10000 bytes)
Starting importing 500 account(s).
✔  Imported successfully.

これでFirebase Authentication のデータを入れ替えることができました。

終わりに

コマンドに渡すオプションを適切に設定していれば簡単にデータを入れ替えることができました。
データを一旦削除するためのコマンドは用意されていないので、スクリプトを実行する必要があります。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?