feat: rest request sync with firestore

This commit is contained in:
Andrew Bastin
2021-08-15 10:45:26 +05:30
parent bb6d9a88ec
commit 7acde1c174
3 changed files with 145 additions and 1 deletions

View File

@@ -1,7 +1,15 @@
import firebase from "firebase/app"
import "firebase/firestore"
import "firebase/auth"
import { BehaviorSubject, Subject } from "rxjs"
import {
BehaviorSubject,
distinctUntilChanged,
filter,
map,
Subject,
Subscription,
} from "rxjs"
import { onBeforeUnmount, onMounted } from "@nuxtjs/composition-api"
export type HoppUser = firebase.User & {
provider?: string
@@ -219,3 +227,29 @@ export async function setProviderInfo(id: string, token: string) {
throw e
}
}
/**
* A Vue composable function that is called when the auth status
* is being updated to being logged in (fired multiple times),
* this is also called on component mount if the login
* was already resolved before mount.
*/
export function onLoggedIn(exec: (user: HoppUser) => void) {
let sub: Subscription | null = null
onMounted(() => {
sub = currentUser$
.pipe(
map((user) => !!user), // Get a logged in status (true or false)
distinctUntilChanged(), // Don't propagate unless the status updates
filter((x) => x) // Don't propagate unless it is logged in
)
.subscribe(() => {
exec(currentUser$.value!)
})
})
onBeforeUnmount(() => {
sub?.unsubscribe()
})
}