使用 @nuxt/auth 进行 Nuxt 身份验证 - 找不到 /api/sessions

时间:2021-01-19 20:22:03

标签: javascript authentication nuxt.js nuxt-auth

我已经尝试了几个小时来实现@nuxt/auth https://auth.nuxtjs.org/

我正在学习本教程:https://www.youtube.com/watch?v=zzUpO8tXoaw&t=978s&ab_channel=VueScreencasts

当我尝试进行 API 调用时,它找不到路由。 POST http://localhost:3000/api/sessions 404 (Not Found)

我正在使用

"nuxt": "^2.14.12"
"@nuxtjs/auth": "^4.9.1",
"@nuxtjs/axios": "^5.1.0",

这是我完整的 nuxt.config.js

export default {
  /*
  ** Nuxt rendering mode
  ** See https://nuxtjs.org/api/configuration-mode
  */
  mode: 'universal',
  /*
  ** Nuxt target
  ** See https://nuxtjs.org/api/configuration-target
  */
  target: 'server',
  /*
  ** Headers of the page
  ** See https://nuxtjs.org/api/configuration-head
  */
  head: {
    title: process.env.npm_package_name || '',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },
  /*
  ** Global CSS
  */
    css: [
        '@/assets/scss/main.scss'
    ],

  /*
  ** Plugins to load before mounting the App
  ** https://nuxtjs.org/guide/plugins
  */
  plugins: [
  ],
  /*
  ** Auto import components
  ** See https://nuxtjs.org/api/configuration-components
  */
  components: true,
  /*
  ** Nuxt.js dev-modules
  */
  buildModules: [
  ],
  /*
  ** Nuxt.js modules
  */
    modules: [
        // Doc: https://bootstrap-vue.js.org
        'bootstrap-vue/nuxt',
        // Doc: https://axios.nuxtjs.org/usage
        '@nuxtjs/axios',
        '@nuxtjs/auth'
    ],
    bootstrapVue: {
        bootstrapCSS: false,
        bootstrapVueCSS: false,
    },
  /*
  ** Axios module configuration
  ** See https://axios.nuxtjs.org/options
  */
    axios: {
        baseURL: 'http://localhost:3000/api',
    },

    auth: {
        strategies: {
            local: {
                endpoints: {
                    login: { url: '/sessions', method: 'post', propertyName: 'token' },
                    logout: { url: 'api/auth/logout', method: 'post' },
                    user: { url: '/sessions/user', method: 'get' }
                },
                tokenType: '',
            }
        }
    },
  /*
  ** Build configuration
  ** See https://nuxtjs.org/api/configuration-build/
  */
  build: {
    }
}

在我的pages/admin.vue

methods: {
    async login() {
        try {
            let response = await this.$auth.loginWith('local', {
                data: {
                    username: this.username,
                    password: this.password,
                }
            })
            console.log(response)
        } catch (err) {
            console.log(err)
        }
    }
},

我错过了什么?我感谢任何帮助

0 个答案:

没有答案