简体   繁体   English

Nuxt-auth v5 模块未在商店中设置登录用户 state

[英]Nuxt-auth v5 module does not set loggedin user in store state

I am currently workin on Authentication functionality with the help of the Nuxt Auth Module.我目前正在 Nuxt Auth 模块的帮助下研究身份验证功能。 On the frontend i am running Nuxt Js, and on the backend i am running FastApi.在前端我运行 Nuxt Js,在后端我运行 FastApi。

In nuxt.config.js i have set the auth settings:在 nuxt.config.js 中,我设置了身份验证设置:

  //Nuxt Auth module configuration https://auth.nuxtjs.org/schemes/local
  auth: {
    rewriteRedirects: false,
    cookie: {
      options: {
          maxAge: 60 * 60 * 60 // 60 hours
      }
    },
    localStorage: {
      prefix: 'auth.'
    },
    strategies: {
      local: {
        token: {
          prefix: 'access_token.',
          property: 'access_token',
          type: 'Bearer',
          maxAge: 60 * 60 * 60
        },
        user: {
          property: 'user',
          autoFetch: true
        },
        endpoints: {
          login: { url: '/api/v1/login/access-token', method: 'post' },
          logout: false,
          user: { url: '/api/v1/users/me', method: 'get' }
        },
        redirect: {
          login: '/login',
          logout: '/',
          // callback: '/login',
          home: '/dashboard'
        }
      }
    }
  }

In my Login.vue i have a form with the login method: import materialCard from '~/components/material/AppCard'在我的 Login.vue 中,我有一个带有登录方法的表单: import materialCard from '~/components/material/AppCard'

export default {
  components: {
    materialCard
  },
  middleware: 'authenticated',
  auth: 'guest',
  data () {
    return {
      username: 'admin',
      password: 'admin'
    }
  },
  methods: {
    async authenticate () {
      const form = new FormData()
      form.append('username', this.username)
      form.append('password', this.password)
      await this.$auth.loginWith('local', { data: form })
        .then((res) => {
          console.log(res)
        }).catch((err) => {
          console.log(err.res)
        })
    }
  }
}

When i try to login, the async function 'login' is being called.当我尝试登录时,正在调用异步 function“登录”。 The user that corresponds with the username and password gets returned.返回与用户名和密码对应的用户。 The only problem i have, is that the when i look in the vuex state, auth.loggedIn stays false and the auth.user stays undefined.我遇到的唯一问题是,当我查看 vuex state 时,auth.loggedIn 保持 false 并且 auth.user 保持未定义。

I thought Nuxt Auth updates the state automatically, or am i missing something?我以为 Nuxt Auth 会自动更新 state,还是我遗漏了什么?

I got the same problem with oauth2, please make sure your redirectUri have auth enabled.我在 oauth2 上遇到了同样的问题,请确保您的 redirectUri 已启用身份验证。 It does not call $auth.fetchUser() when I set redirectUri: '/' because '/' is auth: false .当我设置redirectUri: '/'时它不会调用 $auth.fetchUser() 因为'/'auth: false More of my finding: https://github.com/nuxt-community/auth-module/discussions/1592#discussioncomment-4250173我的更多发现: https://github.com/nuxt-community/auth-module/discussions/1592#discussioncomment-4250173

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

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