Skip to content

Commit f640929

Browse files
Soviutposva
andauthored
feat(auth): useIsCurrentUserLoaded() composable (#1307)
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
1 parent ecfdf4c commit f640929

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

docs/guide/auth.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const user = useCurrentUser()
4848

4949
### Wait for the user to be loaded
5050

51+
The `useCurrentUser()` composable will give you an `undefined` value until the user is loaded. It will then become `null` or the user object itself. If you need to wait for the user to be loaded in a declarative fashion, you can use the `useIsCurrentUserLoaded()` composable. Internally it's just a computed property that returns `true` when if user is not `undefined`.
52+
5153
There is also a `getCurrentUser()` function that returns a promise of the current user. This is useful if you want to wait for the user to be loaded before doing anything. You can, for example, await it within a navigation guard:
5254

5355
```ts

src/auth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { authUserMap, setupOnAuthStateChanged } from './user'
88

99
export {
1010
useCurrentUser,
11+
useIsCurrentUserLoaded,
1112
getCurrentUser,
1213
updateCurrentUserProfile,
1314
updateCurrentUserEmail,

src/auth/user.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
reauthenticateWithCredential,
99
AuthCredential,
1010
} from 'firebase/auth'
11-
import type { Ref } from 'vue-demi'
11+
import { computed, Ref } from 'vue-demi'
1212
import { useFirebaseApp } from '../app'
1313
import type { _MaybeRef, _Nullable } from '../shared'
1414

@@ -36,6 +36,17 @@ export function useCurrentUser(name?: string) {
3636
return authUserMap.get(useFirebaseApp(name))!
3737
}
3838

39+
/**
40+
* Helper that returns a computed boolean that becomes `true` as soon as the current user is no longer `undefined`. Note
41+
* this doesn't ensure the user is logged in, only if the initial signing process has run.
42+
*
43+
* @param name - name of the application
44+
*/
45+
export function useIsCurrentUserLoaded(name?: string) {
46+
const currentUser = useCurrentUser(name)
47+
return computed(() => currentUser !== undefined)
48+
}
49+
3950
/**
4051
* Updates the current user profile and updates the current user state. This function internally calls `updateProfile()`
4152
* from 'firebase/auth' and then updates the current user state.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export { useFirebaseApp } from './app'
7878
*/
7979
export {
8080
useCurrentUser,
81+
useIsCurrentUserLoaded,
8182
VueFireAuth,
8283
useFirebaseAuth,
8384
getCurrentUser,

0 commit comments

Comments
 (0)