【问题标题】:How to save users sessions with VueJS?如何使用 VueJS 保存用户会话?
【发布时间】:2017-02-09 09:59:36
【问题描述】:

我试图弄清楚如何在用户注销时保存用户会话。例如多个购物车、交易,甚至以前的搜索。我对整个后端语言都很陌生,我只是在寻求有关此事的指导。我已经在 google-foo 上尝试过我的分享,但没有找到任何关于我想要实现的目标的好的文档。

谁能帮助和/或指导我?

【问题讨论】:

    标签: node.js vue.js


    【解决方案1】:

    尝试使用vue-session

    登录区示例:

    export default {
        name: 'login',
        methods: {
            login: function () {
              this.$http.post('http://somehost/user/login', {
                password: this.password,
                email: this.email
              }).then(function (response) {
                if (response.status === 200 && 'token' in response.body) {
                  this.$session.start()
                  this.$session.set('jwt', response.body.token)
                  Vue.http.headers.common['Authorization'] = 'Bearer ' + response.body.token
                  this.$router.push('/panel/search')
                }
              }, function (err) {
                console.log('err', err)
              })
            }
        }
    }
    

    登录区域:

    export default {
      name: 'panel',
      data () {
        return { }
      },
      beforeCreate: function () {
        if (!this.$session.exists()) {
          this.$router.push('/')
        }
      },
      methods: {
        logout: function () {
          this.$session.destroy()
          this.$router.push('/')
        }
      }
    }
    

    【讨论】:

    • 该软件包已经 4 年没有任何活动了。也许最好使用本机javascript?
    【解决方案2】:

    您需要将会话存储在 cookie 中或服务器上。

    vue-cookie 将是一个很好的用于浏览器存储的组件。

    如果您要存储在服务器上,您需要为数据创建一个端点并以某种方式存储它;数据库、缓存文件、redis 等。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      相关资源
      最近更新 更多