[英]Cannot logout user on firebase when he close the page
I'm developing a ERP program in vue/firebase.我正在 vue/firebase 中开发一个 ERP 程序。 My problem is: I need to logout the user when he closes the browser or the page who running the program.
我的问题是:当他关闭浏览器或运行程序的页面时,我需要注销用户。
I tried to use the setPersistence, like the docs here: https://firebase.google.com/docs/auth/web/auth-state-persistence ;我尝试使用 setPersistence,就像这里的文档: https://firebase.google.com/docs/auth/web/auth-state-persistence ; but it keeps returning an error of undefined atributes like here:
但它不断返回未定义属性的错误,如下所示:
[Vue warn]: Error in created hook: "TypeError: Cannot read property 'Persistence' of undefined"
(found in Root)
warn @ vue.esm.js?efeb:628
logError @ vue.esm.js?efeb:1893
globalHandleError @ vue.esm.js?efeb:1888
handleError @ vue.esm.js?efeb:1848
invokeWithErrorHandling @ vue.esm.js?efeb:1871
callHook @ vue.esm.js?efeb:4228
Vue._init @ vue.esm.js?efeb:5017
Vue @ vue.esm.js?efeb:5094
eval @ main.js?3479:46
(anonymous) @ build.js:2965
__webpack_require__ @ build.js:679
fn @ build.js:89
(anonymous) @ build.js:2803
__webpack_require__ @ build.js:679
(anonymous) @ build.js:725
(anonymous) @ build.js:728
and also that:还有:
TypeError: Cannot read property 'Persistence' of undefined
at Vue.created (main.js?3479:51)
at invokeWithErrorHandling (vue.esm.js?efeb:1863)
at callHook (vue.esm.js?efeb:4228)
at Vue._init (vue.esm.js?efeb:5017)
at new Vue (vue.esm.js?efeb:5094)
at eval (main.js?3479:46)
at Object.<anonymous> (build.js:2965)
at __webpack_require__ (build.js:679)
at fn (build.js:89)
at Object.<anonymous> (build.js:2803)
Like i said before i'm using vue.js to develop the solution, and put it on created() function in main.js, here's my code:就像我之前所说的,我使用 vue.js 来开发解决方案,并将其放在 main.js 中的 created() function 上,这是我的代码:
import FirebaseVue from './firebase/index' import Vue from 'vue' import App from './App.vue' import router from './router' import money from 'v-money' import Vuelidate from 'vuelidate' import { BootstrapVue, IconsPlugin,LayoutPlugin,ModalPlugin,CardPlugin,VBScrollspyPlugin,DropdownPlugin, TablePlugin} from 'bootstrap-vue' import { library } from '@fortawesome/fontawesome-svg-core' import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome' import { VueMaskDirective } from 'v-mask' import { faTimesCircle, faPlusCircle, faCoffee, faHome, faLogout, faSignOutAlt, faEdit, faUpload, faDollarSign, faTimes, faClock, faCheck, faChevronLeft, faChevronRight, faArrowCircleLeft, faArrowCircleRight, } from '@fortawesome/free-solid-svg-icons' library.add(faTimesCircle,faPlusCircle,faCoffee,faHome,faSignOutAlt,faEdit,faUpload,faDollarSign,faTimes,faClock,faCheck,faChevronLeft,faChevronRight,faArrowCircleLeft,faArrowCircleRight) Vue.component('font-awesome-icon', FontAwesomeIcon) Vue.use(BootstrapVue) Vue.use(IconsPlugin) Vue.use(LayoutPlugin) Vue.use(ModalPlugin) Vue.use(CardPlugin) Vue.use(VBScrollspyPlugin) Vue.use(DropdownPlugin) Vue.use(TablePlugin) Vue.use(FirebaseVue) Vue.use(require('vue-moment')); Vue.use(money, {precision: 4}) Vue.use(Vuelidate) new Vue({ router, render: h=>h(App), created(){ this.$firebase.auth().setPersistence(this.$firebase.auth.Auth.Persistence.SESSION), this.$firebase.auth().onAuthStateChanged( user => { let id_cliente = localStorage.getItem('id_cliente'); let firebase = this.$firebase.firestore(); if (user) { firebase.collection('sessoes_usuarios').doc(id_cliente).update({status:true}).then( console.log('usuário logado com sucesso') ).catch((e)=>{ console.log(e); console.log('não possui log de sessão') }); } else { firebase.collection('sessoes_usuarios').doc(id_cliente).update({status:false}).then( console.log('usuário deslogado com sucesso') ).catch((e)=>{ console.log(e); console.log('não possui log de sessão') }); } }) } }).$mount('#app')
Here's the firebase/index.js folder content:这是 firebase/index.js 文件夹内容:
import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/storage'
import 'firebase/firestore'
export const firebaseApp = firebase.initializeApp({
apiKey: xxx,
authDomain: xxx,
databaseURL: xxx,
projectId: xxx,
storageBucket: xxx,
messagingSenderId: xxx,
appId: xxx
})
export default function install (Vue) {
Object.defineProperty(Vue.prototype, '$firebase', {
get(){
return firebaseApp
}
})
}
Am i missing somenthing?我错过了什么吗?
I found the error, the problem is that i doens't import the auth library:我发现了错误,问题是我没有导入 auth 库:
import 'firebase/auth'
I don't received any error logs before it.在此之前我没有收到任何错误日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.