【问题标题】:Cannot logout user on firebase when he close the page关闭页面时无法在firebase上注销用户
【发布时间】:2020-11-13 13:52:59
【问题描述】:

我正在 vue/firebase 中开发一个 ERP 程序。我的问题是:当用户关闭浏览器或运行程序的页面时,我需要注销用户。

我尝试使用 setPersistence,就像这里的文档:https://firebase.google.com/docs/auth/web/auth-state-persistence; 但它不断返回未定义属性的错误,如下所示:

[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

还有:

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)

就像我之前所说的,我使用 vue.js 开发解决方案,并将其放在 main.js 中的 created() 函数中,这是我的代码:

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')

这是 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
    }
  })
}

我错过了什么吗?

【问题讨论】:

  • 在 Stack Overflow 上,请不要显示文字图片。将文本复制到问题中,以便阅读、复制和搜索。
  • @DougStevenson 好的,我是新来的。谢谢你的建议!我现在正在改变!
  • 您能否提供更多关于this.$firebasefirebase 的信息(例如firebase.collection('sessoes_usuarios'))并分享/firebase/index 的内容
  • @RenaudTarnec 我们使用 'this.$firebase' 因为我们有一个目录,其中包含导入的凭据和 firebase 方法。因此,当我们调用 firebase 时,我们必须像“vue 函数调用”一样进行操作。我将编辑 firebase/index 目录。 firebase.collection('sessoes_usuarios') 是一个用来控制当前谁登录或不登录的集合,它对我们来说就像一个“状态管理器”。
  • 你不使用FirebaseVue from import FirebaseVue from './firebase/index'?阿洛斯,this.$firebase.auth().onAuthStateChanged 被触发了吗?

标签: javascript firebase vue.js firebase-authentication


【解决方案1】:

我发现了错误,问题是我没有导入授权库:

import 'firebase/auth'

之前我没有收到任何错误日志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多