パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.1.0

firebase_user_stream

このパッケージは、firebase_authに欠けている機能を提供することを目的としています。すなわち、サーバーからユーザー情報を再読み込みし、新しいデータを含むストリーム更新を取得できるようにすることです。

30日間ダウンロード
41
いいね数
20
Pub ポイント
40

パッケージをインストール

flutter pub add firebase_user_stream

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

firebase_user_stream パッケージは、サーバーからリアルタイムでユーザー情報を再読み込みし、更新されたユーザー情報のストリームを提供することで、Firebase Auth を拡張します。これは、firebase_auth がユーザー情報のリフレッシュおよびライブアップデートの組み込みサポートを欠いているというギャップを埋めます。特に、サーバー上でユーザー情報が変更された場合に、アプリのセッション間で正確なユーザー状態を維持するのに役立ちます。このパッケージは既存の Firebase Auth ワークフローとシームレスに統合され、UI コンポーネントが最新のユーザー詳細を反映するための手動のリフレッシュロジックを必要としません。

用途

  • リアルタイムユーザー データ同期
  • サーバーサイドの更新後にユーザー情報を更新する
  • UIでユーザー状態を最新の状態に保つ
  • リアルタイムでのユーザープロファイルの変更処理
  • 認証フローにおける古くなったユーザーデータの回避

主な機能

  • ストリームベースのユーザー更新
  • サーバーサイドユーザー データの再読み込み
  • firebase_auth とのシームレスな統合
  • 認証状態の変更後に自動同期
  • 軽量で焦点を当てた機能

適している対象

  • ライブユーザー データを必要とするアプリ
  • リアルタイム更新が必要なFirebase Authユーザー
  • 手動のユーザー更新ロジックを避ける開発者
  • 動的なユーザー・プロファイルを持つプロジェクト
  • Firebase を使用したクロスプラットフォームアプリ

注意事項

  • firebase_auth を設定する必要があります
  • ユーザー データの更新のみに制限
  • 追加の認証機能なし
  • Firebaseバックエンドの安定性に依存します
  • ログイン後のユーザー情報が変更されない場合は不要

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Firebase User Stream

pub package

A Package for subscribing to User reload updates.

ATTENTION

Since firebase_auth version 0.18.0 this library is mostly pointless as firebase_auth (finally) adds authStateChanges() which mostly substitutes this package, fixing the problem described below.

I mean mostly because here we can have further functionality like:

  • Reloading based on a predicate
  • Reload and get the user in the same line.

Although that's not useful to most people.

The (old, pre firebase_auth version 0.18.0) problem

The FirebaseAuth Flutter plugin provides a Stream<FirebaseUser> with onAuthStateChanged, which is useful for getting updates when a user signs-in or signs-out, but it fails to provide an update when the user data itself changes.

A common problem is when we want to detect if the user has just verified his e-mail address, in other words get if FirebaseUser.isEmailVerified changed its value, as this is updated server-side and not pushed to the app.

FirebaseAuth.currentUser() will only detect changes made locally to the user, but if any server-side changes occur, it won't detect them, unless FirebaseUser.reload() is called first, then we need to call FirebaseAuth.currentUser() again to get the reloaded user, but still this user won't be emitted by onAuthStateChanged.

The solution

There are two ways we can get a new FirebaseUser, with the latest information on the server:

  1. Do it manually:
Future<FirebaseUser> reloadCurrentUser() async {
  FirebaseUser oldUser = await FirebaseAuth.instance.currentUser();    
  oldUser.reload();
  FirebaseUser newUser = await FirebaseAuth.instance.currentUser();
  // Add newUser to a Stream, maybe merge this Stream with onAuthStateChanged?
  return newUser; 
}
  1. Let us do it for you!

Getting Started

After you install this package you can use FirebaseUserReloader to get reload updates using the onUserReloaded and onAuthStateChangedOrReloaded streams.

FirebaseUserReloader internally uses the FirebaseAuth instance returned by FirebaseAuth.instance, so your entire App will have the same Singleton instance, you don't have to configure anything, it just works out of the box.

In order to get reload updates just do:

var subscription = FirebaseUserReloader.onUserReloaded.listen((user) {
  // A new user will be printed each time there's a reload
  print(user);
});

// This will trigger a reload and the reloaded user will be emitted by onUserReloaded
FirebaseUserReloader.reloadCurrentUser();

subscription.cancel();

Each time you call reloadCurrentUser() a reloaded user will be emitted by onUserReloaded.

Keep in mind that onUserReloaded will emit reloaded users, they might be new, or just the current one if the user data didn't change.

As a bonus, you can provide a predicate to reloadCurrentUser(), in order to only get reloaded users that matter to you:

var subscription = FirebaseUserReloader.onUserReloaded.listen((user) {
  // A new user will be printed each time there's a reload
  print(user);
});

// This will trigger a reload and the reloaded user will be emitted by onUserReloaded
// only if isEmailVerified == true
FirebaseUserReloader.reloadCurrentUser((user) => user.isEmailVerified);

subscription.cancel();

You also can get the reloaded user as the return value of reloadCurrentUser, but in this case, the predicate will be ignored, and the reloaded user will always be returned.

var user = await FirebaseUserReloader.reloadCurrentUser();

If you want to listen for updates on sign-ins, sign-outs and user reloads, use onAuthStateChangedOrReloaded instead, it's just a convenient merge of onUserReloaded and FirebaseAuth.onAuthStateChanged and it works as a broadcast Behavior Subject:

var subscription = FirebaseUserReloader.onAuthStateChangedOrReloaded.listen((user) {
  // A new user will be printed each time there's a reload, login or logout
  print(user);
});

// This will trigger a reload and the reloaded user will be emitted by onUserReloaded
// only if isEmailVerified == true
FirebaseUserReloader.reloadCurrentUser((user) => user.isEmailVerified);

subscription.cancel();

Both onAuthStateChangedOrReloaded and onUserReloaded are broadcast streams.

Testing

This library uses static methods for easiness of usage, but this doesn't limit its testability.

FirebaseUserReloader can be injected with a mocked instance of FirebaseAuth for unit testing.

For any examples on how to control its behavior under tests, please take a look at our own tests inside the test folder.

カテゴリー

依存関係

依存関係一覧 2 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。