authentication - Nuxt Auth - 未设置用户数据

标签 authentication axios vuex nuxt.js

我尝试通过 nuxt-auth 模块登录。作为响应,我得到了 token ,然后传递了用户数据。但是,this.$Auth.loggedInfalsethis.$Auth.userundefined .我已经战斗了3天,不能再继续下去了。希望有人能帮助我。

登录

await this.$auth.login({
    data: {
        email: this.email,
        password: this.password
    }
}).then(() => {
    this.$router.push('/dashboard')
}).catch(err => {
    this.snackbar.show = true;
})

nuxt.config.js
auth: {
    strategies: {
        local: {
            endpoints: {
                login: {
                    url: '/auth/login',
                    method: 'post',
                    propertyName: 'access_token'
                },
                logout: {
                    url: '/auth/logout',
                    method: 'post'
                },
                user: {
                    url: '/auth/me',
                    method: 'post'
                },
                tokenRequired: true
            }
        }
    }
}

回复登录
{
"access_token": "xxxxxxxxxxxxx.eyJpc3MiOiJodHRwczpcL1wvYXBpLmFwcHJlexxxxxxxcxXRoXC9sb2dpbiIsImlhdCI6MTUzODI5NTczMywiZXhwIjoxNTM4Mjk5MzMzLCJuYmYiOjE1MzgyOTU3MzMsImp0aSI6ImdtWWVyZTViQjk1cU5BRG8iLCJzdWIiOjIsInBydiI6IjYwODM2NzQ0MzQ4ZDQzMTk4NzE4N2ZjMWM2YzIzMjYxMDcyMWE5ZjAifQ.JhOiwIg7StzZR71aqYyI9rJpPXVclmddzPSIwqCIUN4",
"token_type": "bearer",
"expires_in": 3600
}

回复用户
{
    "id": 2,
    "name": "Dominik Dummy",
    "email": "dummy@andreas-pabst.de",
    "created_at": {
        "date": "2018-09-28 09:11:31.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    "updated_at": {
        "date": "2018-09-28 09:11:31.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    "self": "https:\/\/api.apprex.de\/api\/users\/2"
}

enter image description here

最佳答案

好的,经过长时间的尝试,我终于解决了。问题是 auth.fetchUser()需要属性 user在用户响应中,这在我的用户响应中不存在。我在 nuxt.config.js 中将 propertyName 设置为 false,现在它可以工作了

*nuxt.config.js

auth: {
strategies: {
    local: {
        endpoints: {
            login: {
                url: '/auth/login',
                method: 'post',
                propertyName: 'access_token'
            },
            logout: {
                url: '/auth/logout',
                method: 'post'
            },
            user: {
                url: '/auth/me',
                method: 'post',
                propertyName: false // <--- Default "user"
            }
        }
    }
}
}

目前我不确定这是否是模块中的错误

关于authentication - Nuxt Auth - 未设置用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52576160/

相关文章:

express - req.isAuthenticated() 始终为 false

c# - 如何将信息从 OnAuthenticated 事件传递到 Controller 和登录?

node.js - docker 与 axios (node) 组成网络错误 ENOTFOUND

typescript - axios没有导出成员 'CancelToken'

javascript - 如何在 vuex 商店中使用 vue-resource ($http) 和 vue-router ($route)?

vue.js - 如何使用 Vue 3 组件内的 Vuex 4 状态数据作为另一个调度的有效负载?

vue.js - 使用 Vue 在 v-for 循环中将可迭代元素作为函数参数传递

如果未通过异常身份验证,AngularJS 会重定向到登录页面

vue.js - vuejs axios 下载获取文件名

codeigniter - Phonegap 和 Jquery 移动应用程序使用 Codeigniter Ion Auth 后端进行登录