Last active
August 19, 2019 12:31
-
-
Save Yemolai/00f22bd5a9bae5a8e8261d687354e11b to your computer and use it in GitHub Desktop.
plugin para uso fácil de Firebase em app Vue
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 Vue from 'vue' | |
import firebase from 'firebase/app' | |
import 'firebase/auth' | |
import 'firebase/firestore' | |
import router from '@/router' | |
import store from '@/store' | |
if (!process.env.FIREBASE_CONFIG) { | |
throw new Error('firebase config not found in env') | |
} | |
// const { router, store } = Vue.prototype | |
const config = JSON.parse(process.env.FIREBASE_CONFIG) | |
firebase.initializeApp(config) | |
const auth = firebase.auth() | |
const database = firebase.firestore() | |
Vue.prototype.$auth = auth | |
Vue.prototype.$database = database | |
auth.onAuthStateChanged(async user => { | |
if (user !== store.state.auth.user) { | |
await store.dispatch('auth/update', { user: user }) | |
} | |
if (user !== null && router.currentRoute.name === 'login') { | |
const db = firebase.firestore() | |
const userDocumentRef = db.collection('users').doc(user.uid) | |
const userDocumentSnapshot = await userDocumentRef.get() | |
.catch(err => { | |
console.error('error:', err) | |
return null | |
}) | |
if (userDocumentSnapshot && userDocumentSnapshot.exists) { | |
const userDocument = userDocumentSnapshot.data() | |
const { id: roleRawUid = process.env.FALLBACK_ROLE_UID } = userDocument.role | |
const roleUid = roleRawUid.replace(/\n\s+/g, '').trim() | |
const roleSnapshot = await db.collection('roles').doc(roleUid).get() | |
.catch(err => { | |
alert('Não foi possível obter papel do usuário:' + err.message) | |
auth.signOut() | |
throw err | |
}) | |
const role = { ...roleSnapshot.data(), id: roleSnapshot.id }; | |
store.dispatch('auth/setRole', { role }) | |
} else { | |
const defaultRoleUid = process.env.FALLBACK_ROLE_UID | |
const roleUid = defaultRoleUid.replace(/\n\s+/g, '').trim() | |
const roleDocumentRef = db.collection('roles').doc(roleUid) | |
const roleDocumentSnapshot = await roleDocumentRef.get() | |
.catch(err => { | |
alert('Não foi possível obter papel do usuário:' + err.message) | |
auth.signOut() | |
throw err | |
}) | |
const role = roleDocumentSnapshot.data() | |
store.dispatch('auth/setRole', { role }) | |
} | |
const { redirect = { name: 'Dashboard' } } = router.currentRoute.query | |
router.push(redirect) | |
} else if (user === null && router.currentRoute.name !== 'login') { | |
router.push({ name: 'login' }) | |
} | |
}) | |
auth.onIdTokenChanged(user => { | |
if (user !== null) { | |
if (!auth.currentUser) { | |
return | |
} | |
auth.currentUser.getIdToken(true) | |
.then(token => { | |
store.dispatch('auth/token', { token }) | |
}) | |
.catch(err => { | |
console.error('error while updating token:', err) | |
store.dispatch('auth/token', { token: null }) | |
}) | |
} else { | |
store.dispatch('auth/token', { token: null }) | |
} | |
}) | |
router.beforeEach((to, _from, next) => { | |
if (to.matched.some(record => record.meta.requiresAuth)) { | |
if (auth.currentUser) { | |
const permissions = store.getters['auth/permissions'] || [] | |
const needPermission = to.meta.authRole || false | |
const hasPermission = needPermission ? permissions.indexOf(needPermission) > -1 : true | |
if (!hasPermission) { | |
next({ path: '/' }) | |
} | |
next() | |
} else { | |
next({ | |
path: '/login', | |
query: { redirect: to.fullPath } | |
}) | |
} | |
} else { | |
next() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment