Nuxt 登录权限配置

2,006 阅读1分钟
安装Auth Module npm install @nuxtjs/auth
在nuxt.config.js配置文件中配置auth
 modules: [
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    '@nuxtjs/pwa',
    '@nuxtjs/auth'
  ],

auth: {
    strategies: {
      local: {
        endpoints: {
          logout: { url: '/user/logout', method: 'post' },
          user: {
            url: '/user/currentUser',
            method: 'get',
            propertyName: 'data'
          }
        }
      }
    }
  },
 router: {
    linkPrefetchedClass: 'nuxt-link-prefetched',
    linkExactActiveClass: 'exact-active-link',
    middleware: ['auth'],  //配置中间件auth
    prefetchLinks: false
  }
 在登录页面登录时
  setUserToken(token: any) {//登录时获取的token传进来
    ;(this as any).$auth.setUserToken(token).then(() => {
    //判断用户的权限可以看哪些模块
      this.$router.push(
        (this as any).$auth.user.roleType === 3 ? '/homework' : '/'
      )
    })
  }