📑

FirebaseAuthのuserChangesドキュメントが紛らわしい【 Flutter✖️Firebase 】

2023/02/11に公開
FirebaseAuth.instance.userChanges

ってコード見た事ありますか?

これは、firebaseUserのデータをStreamしてくれる優れものさんです。

こいつのリファレンスです↓
https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth/userChanges.html

翻訳面倒だと思うので、僕の方でしますが下記の通りです(deeplyから)

ユーザー更新の変更について通知します。
これは、authStateChanges と idTokenChanges の両方のスーパーセットです。 資格情報がリンクされたとき、リンクが解除されたとき、ユーザー プロファイルが更新されたときなど、すべてのユーザーの変更に関するイベントを提供します。 このストリームの目的は、ユーザーの状態 (サインイン、サインアウト、別のユーザーとトークンの更新) に対するリアルタイムの更新をリッスンすることです。手動でreloadを呼び出して、アプリケーションへの変更をリハイドレートする必要はありません。

要するに、ここでのドキュメントでは、

await Firebase.instance.currentUser?.reload;

こいつが要らないよって言ってる気がします。
というか、まさしくそうですよね。

ですが、実際に使う現場では普通に必要です。

それが確認できたのが、メール認証チェックです。
emailVerifiedみたいな認証メールのリンクを開いたかどうかをチェックできるbool値があるんですが
Streamで監視しても全く反応しませんでした。

え、反応せんのかい!!って感じでしたが、どうやら、、、

https://firebase.flutter.dev/docs/auth/usage/#:~:text=updateProfile()-,Warning%3A,-idTokenChanges()%2C

Warning: idTokenChanges(), userChanges() & authStateChanges() will not fire if you update the User profile via your own firebase admin sdk implementation. You will have to force a reload using the following FirebaseAuth.instance.currentUser.reload() to retrieve the latest User profile.
idTokenChanges(), userChanges() & authStateChanges() will also not fire if you disable or delete the User via your own firebase admin sdk implementation or the Firebase console. You will have to force a reload using the following FirebaseAuth.instance.currentUser.reload() which will cause a user-disabled or user-not-found exception that you can catch and handle in your app code.

翻訳:警告: idTokenChanges()、userChanges()、および authStateChanges() は、独自の firebase admin SDK 実装を介してユーザー プロファイルを更新すると起動しません。 最新のユーザー プロファイルを取得するには、次の FirebaseAuth.instance.currentUser.reload() を使用して強制的にリロードする必要があります。
idTokenChanges()、userChanges()、および authStateChanges() は、独自の firebase admin sdk 実装または Firebase コンソールを介してユーザーを無効化または削除した場合にも起動しません。 次の FirebaseAuth.instance.currentUser.reload() を使用してリロードを強制する必要があります。これにより、アプリ コードでキャッチして処理できるユーザー無効またはユーザーが見つからない例外が発生します。

ズコーっっっっっっっっって感じですよね笑

おいおい、まさかのreloadしないと取得出来へんのかよ!!
って感じでした。

しかも、userを消されてもlistenしないなんて、、、
もはやStreamでも何でもないというw

ということで、毎回Timer.periodとかで3秒後設定でrelaodする必要性があるみたいで
超絶面倒ですね。笑

結論

公式のリファレンスが間違ってるのを見つけると嬉しい気持ちになる。
これが人間の不粋で悪いところなんだろうなぁ。それが良いのかも。知らんけど。

Discussion