Skip to content

Instantly share code, notes, and snippets.

@razbakov
Created April 15, 2020 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razbakov/20610798854af0d90b881ed19d6f8b4a to your computer and use it in GitHub Desktop.
Save razbakov/20610798854af0d90b881ed19d6f8b4a to your computer and use it in GitHub Desktop.
import { reactive, toRefs, computed } from '@vue/composition-api'
import Vue from 'vue'
import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/firestore'
const state = Vue.observable({
loading: true,
signingIn: false,
uid: null,
profile: null,
account: null,
initialized: false
})
export const useAuth = () => {
if (!state.initialized) {
firebase.auth().onAuthStateChanged(setUser)
state.initialized = true
}
async function setUser(user) {
if (user) {
state.uid = user.uid
} else {
state.uid = null
}
state.loading = false
}
}
export const useDoc = (name) => {
const { uid } = useAuth()
const firestore = firebase.firestore()
const collection = firestore.collection(name)
async function update(id, data) {
const result = await collection.doc(id).update(data)
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment