Skip to content

feat(auth): useIsCurrentUserLoaded() composable #1307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const user = useCurrentUser()

### Wait for the user to be loaded

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`.

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:

```ts
Expand Down
1 change: 1 addition & 0 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { authUserMap, setupOnAuthStateChanged } from './user'

export {
useCurrentUser,
useIsCurrentUserLoaded,
getCurrentUser,
updateCurrentUserProfile,
updateCurrentUserEmail,
Expand Down
13 changes: 12 additions & 1 deletion src/auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
reauthenticateWithCredential,
AuthCredential,
} from 'firebase/auth'
import type { Ref } from 'vue-demi'
import { computed, Ref } from 'vue-demi'
import { useFirebaseApp } from '../app'
import type { _MaybeRef, _Nullable } from '../shared'

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

/**
* Helper that returns a computed boolean that becomes `true` as soon as the current user is no longer `undefined`. Note
* this doesn't ensure the user is logged in, only if the initial signing process has run.
*
* @param name - name of the application
*/
export function useIsCurrentUserLoaded(name?: string) {
const currentUser = useCurrentUser(name)
return computed(() => currentUser !== undefined)
}

/**
* Updates the current user profile and updates the current user state. This function internally calls `updateProfile()`
* from 'firebase/auth' and then updates the current user state.
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export { useFirebaseApp } from './app'
*/
export {
useCurrentUser,
useIsCurrentUserLoaded,
VueFireAuth,
useFirebaseAuth,
getCurrentUser,
Expand Down