简体   繁体   English

Nuxt auth 模块未将登录用户设置为商店状态

[英]Nuxt auth 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 身份验证模块的帮助下研究身份验证功能。

On the frontend i am running Nuxt Js, and on the backend i am running Laravel 5.7在前端我运行 Nuxt Js,在后端我运行 Laravel 5.7

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

  auth: {
  strategies: {
      local: {
          endpoints: {
              login: { url: 'login', method: 'post', propertyName: 'access_token' },
              logout: { url: 'logout', method: 'post' },
              user: { url: 'user', method: 'get', propertyName: 'user' },
          }
      },
       tokenRequired: true,
       tokenType: 'bearer',
  }

}, },

in my index.vue i have a form with the login method:在我的 index.vue 中,我有一个带有登录方法的表单:

        <template>
    <div>
      <div>
        <b-container>
          <b-row no-gutters>
            <b-col col lg="12">

            </b-col>
          </b-row>
          <b-row no-gutters>
            <b-col col lg="12">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm" label-position="top">
                  <el-form-item label="Email" prop="email">
                    <el-input v-model="ruleForm.email" ></el-input>
                  </el-form-item>
                  <el-form-item label="Password" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="login">Inloggen</el-button>
                    <!--<el-button @click="resetForm('ruleForm2')">Reset</el-button>-->
                  </el-form-item>
                </el-form>
            </b-col>
          </b-row>
        </b-container>
      </div>
    </div>
    </template>

    <script>
        export default {
            layout: 'login',
            data() {
                var validatePass = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('Vul een wachtwoord in'));
                    } else {
                        callback();
                    }
                };
                return {
                    ruleForm: {
                        email: '',
                        password: '',
                    },
                    rules: {
                        password: [
                            { validator: validatePass, trigger: 'blur' }
                        ],
                        email: [
                            { required: true, message: 'Vul een email in', trigger: 'blur' },
                            { type: 'email', message: 'Vul een correct email adres in', trigger: ['blur'] }
                        ]
                    }
                };
            },
            methods: {

                async login() {
                    try {
                        await this.$auth.loginWith('local', {
                            data: {
                                username: this.ruleForm.email,
                                password: this.ruleForm.password
                            }
                        }).then(() => {
                                this.$router.push({ name: 'dashboard'})
                            })
                    } catch (e) {
                        console.log(e)
                    }
                },
            }
        }
    </script>

When i try to login, the async function 'login' is being called.当我尝试登录时,正在调用异步函数“登录”。 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 状态时,auth.loggedIn 保持为 false,而 auth.user 保持未定义。

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

Any help would be greatly appreciated :D任何帮助将不胜感激:D

I finally found the solution.我终于找到了解决方案。 I had to set the propertyname of the user endpoint to false我必须将用户端点的属性名称设置为 false

Old version旧版本

      endpoints: {
          login: { url: 'login', method: 'post', propertyName: 'access_token' },
          logout: { url: 'logout', method: 'post' },
          user: { url: 'user', method: 'get', propertyName: 'user' },
      }

New version新版本

      endpoints: {
          login: { url: 'login', method: 'post', propertyName: 'access_token' },
          logout: { url: 'logout', method: 'post' },
          user: { url: 'user', method: 'get', propertyName: false },
      }

Now the user gets loaded in the state现在用户被加载到状态

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

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