1
2

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 3 years have passed since last update.

CurrentUserを変更せずにFirebaseのユーザーを追加する

Last updated at Posted at 2022-05-21

本来FirebaseのAuthenticationは各々がアカウント作成を行うのが鉄則です。
しかし、アプリによっては管理者などが代わりにアカウントを作り、
まとめて管理するといった場合を想定しています。

本来の登録

以下処理でFirebase Authenticationにアカウントが作成され
FirebaseAuth.instance.currentUserが切り替わります。
そのため、プロフィールなどのログイン情報で設定しているものが変更されてしまいます。

final userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
        email: email!, password: password!,
      );

対応方法

FirebaseAppの2つ目のインスタンスを作る必要があります。アカウントを作成した後に
インスタンスを削除することで、CurrentUserが自動で反映されるのを防いでいます。

FirebaseApp app = await Firebase.initializeApp(name: 'secondary', options: Firebase.app().options);
final userCredential = await FirebaseAuth.instanceFor(app: app)
      .createUserWithEmailAndPassword(email: email!, password: password!);

app.delete();

注意点

管理者が作成するといっても、第三者がパスワードなどを作成するので安全ではありません。
推奨されている方法ではないことを念頭に置いて使用してください。

参考: creating user is changing the current user

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?