Skip to content

Commit 3875691

Browse files
prescottpruejhuleatt
authored andcommitted
feat(auth): set auth.currentUser as its startWithValue in useUser - #158 (#172)
* feat(auth): set auth.currentUser as its startWithValue in useUser - #158 * fix(useUser): prevent flicker by only skipping suspending if auth.currentUser returns a user - [#172](#172)
1 parent d96bda4 commit 3875691

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

reactfire/auth/index.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ export function useUser<T = unknown>(
3636
): User | T {
3737
auth = auth || getAuthFromContext();
3838

39-
return useObservable(
40-
user(auth),
41-
'auth: user',
42-
options ? options.startWithValue : undefined
43-
);
39+
let currentUser = undefined;
40+
41+
if (options && options.startWithValue !== undefined) {
42+
currentUser = options.startWithValue;
43+
} else if (auth.currentUser) {
44+
// if auth.currentUser is undefined or null, we won't use it
45+
// because null can mean "not signed in" OR "still loading"
46+
currentUser = auth.currentUser;
47+
}
48+
49+
return useObservable(user(auth), 'auth: user', currentUser);
4450
}
4551

4652
export function useIdTokenResult(user: User, forceRefresh: boolean = false) {

0 commit comments

Comments
 (0)