Created
April 15, 2020 12:26
-
-
Save razbakov/20610798854af0d90b881ed19d6f8b4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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