firebase_user_stream 패키지는 실시간으로 서버에서 사용자 데이터를 다시 로드하고 업데이트된 사용자 정보의 스트림을 제공함으로써 Firebase Auth를 확장합니다. 이는 firebase_auth가 사용자 데이터를 새로 고치고 실시간 업데이트를 제공하는 내장 지원이 부족한 점을 메우는 것입니다. 특히 사용자 데이터가 서버에서 변경될 경우 앱 세션 간에 정확한 사용자 상태를 유지하는 데 매우 유용합니다. 이 패키지는 기존의 Firebase Auth 워크플로와 원활하게 통합되며, UI 컴포넌트가 최신 사용자 정보를 반영하기 위해 수동으로 새로 고침 로직을 구현할 필요 없도록 보장합니다.
사용 사례
실시간 사용자 데이터 동기화
서버 측 업데이트 후 사용자 데이터 새로 고침
UI에서 사용자 상태를 최신 상태로 유지하기
실시간 사용자 프로필 변경 처리
인증 흐름에서 오래된 사용자 데이터 피하기
주요 기능
스트림 기반 사용자 업데이트
서버 측 사용자 데이터 재로드
firebase_auth와의 원활한 통합
인증 상태 변경 후 자동 동기화
가볍고 집중된 기능
적합한 대상
실시간 사용자 데이터가 필요한 앱
실시간 업데이트가 필요한 Firebase Auth 사용자
수동 사용자 새로 고침 로직을 피하는 개발자
동적 사용자 프로필이 있는 프로젝트
Firebase를 사용한 크로스 플랫폼 앱
고려 사항
firebase_auth 설정이 필요합니다
사용자 데이터 새로 고침에만 제한됨
추가 인증 기능 없음
Firebase 백엔드 안정성에 따라 달라집니다
로그인 후 사용자 데이터가 변경되지 않는 경우 필요하지 않음
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Firebase User Stream
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:
Do it manually:
Future<FirebaseUser> reloadCurrentUser() async {
FirebaseUser oldUser = awaitFirebaseAuth.instance.currentUser();
oldUser.reload();
FirebaseUser newUser = awaitFirebaseAuth.instance.currentUser();
// Add newUser to a Stream, maybe merge this Stream with onAuthStateChanged?return newUser;
}
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 reloadprint(user);
});
// This will trigger a reload and the reloaded user will be emitted by onUserReloadedFirebaseUserReloader.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 reloadprint(user);
});
// This will trigger a reload and the reloaded user will be emitted by onUserReloaded// only if isEmailVerified == trueFirebaseUserReloader.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 = awaitFirebaseUserReloader.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 logoutprint(user);
});
// This will trigger a reload and the reloaded user will be emitted by onUserReloaded// only if isEmailVerified == trueFirebaseUserReloader.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.