【问题标题】:Firebase + Vue: Disconnected at refresh despite setting persistenceFirebase + Vue:尽管设置了持久性,但刷新时断开连接
【发布时间】:2020-12-17 05:51:12
【问题描述】:

我正在使用 Vue 和 firebase 来构建身份验证机制。每次刷新页面时,身份验证都会被丢弃,我会被重定向到登录页面。

我按照documentation 尝试了setPersistence(firebase.auth.Auth.Persistence.SESSION)。我还实现了这个reddit thread 的一些技巧。我仍然被重定向,我注意到在刷新时,auth.onAuthStateChanged((user) => {}) 回调有user = null。

我有点卡住了。抱歉,如果这是一个新手问题。

firebase.js

import * as firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/firestore'

const firebaseConfig = {
  apiKey: process.env.VUE_APP_API_KEY,
  ...
};

firebase.initializeApp(firebaseConfig)

// utils
const db = firebase.firestore()
const auth = firebase.auth()
auth.setPersistence(firebase.auth.Auth.Persistence.SESSION)


// export utils/refs
export {
  db,
  auth
}

ma​​in.js

import Vue from 'vue'
import App from './App.vue'
import vuetify from './plugins/vuetify';
import router from './router/index'
import { store } from './store'
import { auth } from './firebase'

Vue.config.productionTip = false

let app

auth.onAuthStateChanged((user) => {
  if (!app) {
    app = new Vue({
      router,
      store,
      vuetify,
      render: h => h(App)
    }).$mount('#app')
  }
  if (user) {
    store.dispatch('fetchUserProfile', user)
    store.commit('setEmail', user.email)
  }
  })

路由器/index.js

import Vue from 'vue';
import Router from 'vue-router';
import Login from '../components/Login'
import Dashboard from '../components/Dashboard'
import { auth } from '../firebase'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
      {
          path: '/login',
          name: 'login',
          component: Login,
          meta: {
            disableNavBar: true
          }
      },
      // ... other routes removed for brevity
      {
          path: '/dashboard',
          name: 'Dashboard',
          component: Dashboard,
          meta: {
            requiresAuth: true
        }
      },
    ]
});

router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  if(to["path"] !== from["path"]){
    if(requiresAuth){
      auth.onAuthStateChanged(function(user) {
        if (user) {
          console.log("Logged in", user) 
          next();
        } else {
          console.log("Here", user) //this logs user = null on refresh 
          next('/login')
        }
      });
    } else {
      next();
    }
  }
})

export default router

【问题讨论】:

    标签: firebase vue.js firebase-authentication


    【解决方案1】:

    您需要使用LOCAL 来确保会话在关闭/刷新浏览器选项卡或窗口后仍然存在,而不是使用SESSION 作为身份验证持久性模式。

    所以,在你的 firebase.initializeApp() 旁边,你可以添加这个 -

    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
    

    【讨论】:

    • 谢谢。我也尝试过。它不起作用:我仍然在刷新时被注销。 + 根据文档,SESSION 应该完成这项工作。所以我假设它与其他事情有关。
    • 嗯,奇怪。前段时间我遇到了确切的问题。它适用于本地的东西。我会再看一下,看看是否还有其他问题。见stackoverflow.com/a/62079096/9978232
    【解决方案2】:

    我现在觉得很愚蠢,在这里分享它,因为它可能对面临这个问题的人有用。

    1. 事实证明,我的一个未使用的components.vue 中有一个logout 函数。尽管我的应用程序从未使用过它,但它是由 vue 初始化的。每次发生这种情况(即刷新时),组件都会被初始化,脚本会被执行,store.dispatch("logout") 会被运行。除了将用户会话设置为 null 之外,这还会导致很多无用的重定向。

    出于对这个问题的绝望,我在我的代码库中搜索“logout”,偶然发现了剩余的代码。 “该死的东西一定是让我退出”。那件事只是我自己的疏忽。

    1. 已修复,设置 firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION) 工作正常。

    对于那些花时间回答这个问题的人,谢谢你,对不起:)。万一有人偶然发现了这个问题,并且有相同的根本原因,很高兴这有帮助。

    【讨论】:

    • 由于您的建议,我选择了一个遗留代码库并发现了问题;只需搜索signOut 的所有实例。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    相关资源
    最近更新 更多