How to save users sessions with VueJS?

后端 未结 2 1701
广开言路
广开言路 2020-12-14 19:16

I am trying to figure out how to save users session when they log out. e.g. multiple shopping carts, transactions, and even previous searches. I am pretty new to the whole b

2条回答
  •  失恋的感觉
    2020-12-14 19:56

    try use vue-session

    example in login area:

    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)
              })
            }
        }
    }
    

    logged in area:

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

提交回复
热议问题