简体   繁体   English

Nuxt Auth Module - 将JWT保存在Vuex商店中

[英]Nuxt Auth Module - save JWT in Vuex store

The login/logout/middleware etc themselves work, but I don't seem to have control over the token. 登录/注销/中间件等本身可以工作,但我似乎无法控制令牌。 I'm trying to save JWT in Vuex store after logging in, but the token is only saved in a cookie and localStorage. 我正在尝试在登录后将JWT保存在Vuex存储中,但令牌仅保存在cookie和localStorage中。 From documentation I understand that support for auth in Vuex is added automatically. 从文档中我了解到,自动添加了对Vuex中的auth的支持。 I didn't define tokenRequired and tokenType in config as according to documentation they are needed for cookie based flow (also adding them did not change anything). 我没有在配置中定义tokenRequiredtokenType ,因为它们是基于cookie的流程所需的文档(同时添加它们并没有改变任何东西)。

nuxt.config.js nuxt.config.js

modules: [
    '@nuxtjs/axios',
    '@nuxtjs/auth'
],
axios: {
    baseURL: 'https://api.example.com/'
},
router: {
    middleware: ['auth']
},
auth: {
    strategies: {
        local: {
            endpoints: {
                login: { url: 'login', method: 'post', propertyName: 'token' },
                logout: { url: 'logout', method: 'post' },
                user: false
            }
        }
    },
    redirect: {
        login: '/login',
        logout: '/',
        callback: '/login',
        home: '/'
    }
},

login function 登录功能

await this.$axios.post('authenticate', {
    email: this.email,
    password: this.password
}).then(response => {
    if (response.success === 'true') {
        this.$auth.setUserToken(response.token)
    } else {
        //alert invalid login
    }
}).catch(error => {
    //alert server error
});

Now when I successfully log in and look at $auth.$state it returns 现在,当我成功登录并查看$ auth。$ state时,它返回

{ "user": {}, "loggedIn": true, "strategy": "local" }

I expect the token to also be saved in $auth . 我希望令牌也可以保存在$auth

I also looked at a question with similar title , but their solution does not work for me, as I am using user: false . 我也看了一个类似标题问题 ,但他们的解决方案对我不起作用,因为我使用的是user: false

I looked at auth-module's default.js file and tried the default values in my nuxt.config.js . 我查看了auth-module的default.js文件并尝试了nuxt.config.js的默认值。 After adding the default into my configuration it started working. 将默认值添加到我的配置后,它开始工作。 So now I was able to disable the cookies and localStorage, while saving JWT into store only. 所以现在我能够禁用cookie和localStorage,同时只将JWT保存到商店中。

auth: {
    strategies: {
        local: {
            endpoints: {
                login: { url: 'login', method: 'post', propertyName: 'token' },
                logout: { url: 'logout', method: 'post' },
                user: false
            }
        }
    },
    redirect: {
        login: '/login',
        logout: '/',
        callback: '/login',
        home: '/'
    },
    cookie: false,
    localStorage: false,
    token: {
        prefix: 'token.'
    },
},

And $auth.$state returns 并且$auth.$state返回

{ "user": {}, "loggedIn": true, "strategy": "local", "token.local": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" }

If anyone has an explanation why the default value did not work and why I had to include it in my configuration, please let me know. 如果有人解释为什么默认值不起作用以及为什么我必须将其包含在我的配置中,请告诉我。 I assume for some reason they have disabled the Vuex saving by default? 我假设由于某种原因他们默认禁用了Vuex保存? Even though the documentation states what can be interpreted as by default token is saved in three locations. 即使文档说明默认令牌可以解释为什么,也会将其保存在三个位置。

Auth tokens are stored in various storage providers (cookie, localStorage, vuex) Auth令牌存储在各种存储提供程序中(cookie,localStorage,vuex)

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

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