我正在try 验证Vue.使用firebase的js应用程序.

我有一个问题,如果试图在登录时直接访问受登录保护的URL,路由将在firebase之前加载并判断身份验证状态.js有时间返回auth响应.这会导致用户跳转到登录页面(而他们已经登录).

如何延迟vue路由导航,直到从firebase检索到身份验证状态?我可以看到firebase将身份验证数据存储在localStorage中,作为初步身份验证判断,是否可以安全地判断它是否存在?理想情况下,最终结果是在用户经过身份验证时显示加载微调器或其他内容,然后他们应该能够访问导航到的页面.

路由/索引.js

let router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/example',
      name: 'Example',
      component: Example,
      beforeEnter: loginRequired
    }
})

function loginRequired (to, from, next) {
  if (authService.authenticated()) {
    next()
  } else {
    next('/login')
  }
}

啊.js

import * as firebase from 'firebase'

var config = {
    // firebase config
}

firebase.initializeApp(config)

var authService = {

  firebase: firebase,
  user: null,

  authenticated () {
    if (this.user == null) {
      return false
    } else {
      return !this.user.isAnonymous
    }
  },

  setUser (user) {
    this.user = user
  },

  login (email, password) {
    return this.firebase.auth().signInWithEmailAndPassword(email, password)
      .then(user => {
        this.setUser(user)
      })
  },

  logout () {
    this.firebase.auth().signOut().then(() => {
      console.log('logout done')
    })
  }
}

firebase.auth().onAuthStateChanged(user => {
  authService.setUser(user)
})

export default authService

应用程序.vue

<template>
  <div id="app">
    <p v-if="auth.user !== null">Logged in with {{ auth.user.email }}</p>
    <p v-else>not logged in</p>
    <router-view v-if="auth.user !== null"></router-view>
  </div>
</template>

<script>
import authService from './auth'

export default {
  name: 'app',
  data () {
    return {
      auth: authService
    }
  }
}
</script>

推荐答案

Firebase总是在启动时触发身份验证状态更改事件,但它不是立即发生的.

为了等待Firebase完成其用户/身份验证初始化,您需要让authService.authenticated返回一个promise .

const initializeAuth = new Promise(resolve => {
  // this adds a hook for the initial auth-change event
  firebase.auth().onAuthStateChanged(user => {
    authService.setUser(user)
    resolve(user)
  })
})

const authService = {

  user: null,

  authenticated () {
    return initializeAuth.then(user => {
      return user && !user.isAnonymous
    })
  },

  setUser (user) {
    this.user = user
  },

  login (email, password) {
    return firebase.auth().signInWithEmailAndPassword(email, password)
  },

  logout () {
    firebase.auth().signOut().then(() => {
      console.log('logout done')
    })
  }
}

您不需要从signInWith...promise 中拨打setUser,因为这将由initializeAuthpromise 处理.

Vue.js相关问答推荐

在VueJS中卸载元素之前是否应该删除JavaScript事件收件箱?

复选框列未在vutify中排序

在 Vue.js 2.6 的 Javascript 中访问 的内容

当try 更新 Vue 3 中的关联数组中的单个元素时,所有值都会更新

组合 API | Vue 路由参数没有被监视

如果单元格不可见,则以编程方式打开行的编辑模式. Ag 网格,Vue.js

vite - 具有相对assets资源 路径的子页面

使用 Laravel 作为后端刷新 Vue.js SPA 的身份验证令牌

从组件的 内的组件调用方法

Vuetify Datatable - 在所有列上启用内容编辑

Vue:需要禁用页面上的所有输入

如何将 Nuxt.js 更新到最新版本

vuex: unknown getter: user

Proxy代理在 Vue 3 的控制台中是什么意思?

npm run dev 和 npm run production 的区别

为 Vue 组件动态添加属性

即使在生产模式下构建,VueJS 也会显示开发模式开发模式消息

如何在两个属性上使用 Vue/Vuetify 自动完成过滤器?

如何在 vue.js 中进行嵌入?

Axios 请求拦截器在我的 vue 应用程序中不起作用