简体   繁体   English

Nuxt 中间件中的重定向功能使状态为空

[英]Redirect function in Nuxt middleware is making state null

I have a Nuxt app in which everything works fine in middleware except when I use redirect.我有一个 Nuxt 应用程序,它在中间件中一切正常,除非我使用重定向。 When I comment the redirect('/admin') line it works fine even the state data is present when console logged.当我评论 redirect('/admin') 行时,即使控制台记录时存在状态数据,它也能正常工作。 As soon as I uncomment the redirect line it makes the state null.一旦我取消对重定向行的注释,它就会使状态为空。 Please help if someone knows this issue.如果有人知道这个问题,请帮忙。 This exact code works in my other projects but not here.这个确切的代码适用于我的其他项目,但不适用于这里。

This is my auth.js file in the middleware folder.这是我在中间件文件夹中的auth.js文件。

export default function ({ store, route, redirect }) {
  const user = store.getters['user/user']
  const blockRouteAdmin = /\/admin\/*/g
  const blockRouteManager = /\/manager\/*/g
  const path = ['/signup', '/login']
  let value = path.includes(route.path)
  if (user) {
    if (user.isAdmin) {    
      if (!route.path.match(blockRouteAdmin)) {
        redirect('/admin')
      }
    }
    if (user.isManager) {
      if (!route.path.match(blockRouteManager)) {
          redirect('/manager')
      }
    }
    if (user.isUser) {
           if (
        route.path.match(blockRouteAdmin) ||
        route.path.match(blockRouteManager) ||
        value
      ) {
        console.log('isUser', user.isUser)
        redirect('/')
      }
    }
  }
  if (!user) {
    if (
      route.path.match(blockRouteAdmin) ||
      route.path.match(blockRouteManager)
    ) {
      redirect('/')
    } else {
      redirect()
    }
  }
}

Here is my nuxt.config.js这是我的nuxt.config.js

export default {
  // Target: https://go.nuxtjs.dev/config-target
  target: 'static',

  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'aitl',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: ['~/plugins/firebaseConfig.js'],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/buefy
    'nuxt-buefy',
    // https://go.nuxtjs.dev/pwa
    '@nuxtjs/pwa',
    // https://go.nuxtjs.dev/content
    '@nuxt/content',
  ],

  // PWA module configuration: https://go.nuxtjs.dev/pwa
  pwa: {
    manifest: {
      lang: 'en',
    },
  },

  // Content module configuration: https://go.nuxtjs.dev/config-content
  content: {},

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {},
}

My index.js inside store.我的index.js在商店里面。

import { vuexfireMutations } from 'vuexfire'
import { getUserFromCookie } from '../helper/index.js'

export const mutations = {
  ...vuexfireMutations,
}

export const actions = {
  async nuxtServerInit({ dispatch, commit }, { req }) {
    try {
      const user = getUserFromCookie(req)
      if (user) {
        await dispatch('user/setUSER', {
          email: user.email,
          isAdmin: user.admin,
          isManager: user.manager,
          isUser: user.user,
          uid: user.user_id,
          name: user.name,
        })
      }
    } catch (err) {
      console.log(err)
    }
  },
}

User.js in store folder商店文件夹中的User.js

import { auth } from '../plugins/firebaseConfig'
import Cookies from 'js-cookie'

export const state = () => ({
  user: null,
})

export const getters = {
  user(state) {
    return state.user
  },
}

export const actions = {
  async userlogin({ dispatch }, user) {
    try {
      const token = await auth.currentUser.getIdToken(true)
      const userInfo = {
        email: user.email,
        isAdmin: user.admin,
        isManager: user.manager,
        isUser: user.user,
        uid: user.uid,
        name: user.displayName,
      }
      Cookies.set('access_token', token)
      await dispatch('setUSER', userInfo)
    } catch (err) {
      console.log(err)
    }
  },

  setUSER({ commit }, user) {
    commit('setUSER', user)
  },
}

export const mutations = {
  setUSER(state, user) {
    state.user = user
  },
}

这个问题是通过从target: 'static'target: 'server' ,也就是镜像另一个工作项目的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM