Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save users sessions with VueJS?

Tags:

node.js

vue.js

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 backend language and I am just seeking guidance on the matter. I have tried my share at google-foo for this matter, but have not found any good documentation on what I am trying to achieve.

Can anyone help and/or guide me?

like image 265
Kevin Conklin Avatar asked Sep 30 '16 18:09

Kevin Conklin


People also ask

How do you save a Vue session?

You need to either store the session in a cookie or on the server. vue-cookie would be a good component to use for browser storage. if you're storing on the server you need to create an endpoint for the data and store it in some fashion; a database, cache file, redis, etc.

How do you save on Vue local storage?

vue . Local storage uses the setItem() method to save data as key-value pairs, the data must be a string so we convert the JSON into a string in order to save it from using JSON. stringify() method.

How do I store data in VUE JS cookies?

Installing the vue-cookies package Run the following command to install it. Now, open your main. js file add the following (highlighted) configuration. import Vue from "vue"; import VueCookies from 'vue-cookies';import App from "./App.


2 Answers

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('/')
    }
  }
}
like image 192
Pengguna Avatar answered Sep 20 '22 16:09

Pengguna


You need to either store the session in a cookie or on the server.

vue-cookie would be a good component to use for browser storage.

if you're storing on the server you need to create an endpoint for the data and store it in some fashion; a database, cache file, redis, etc.

like image 21
Justin MacArthur Avatar answered Sep 19 '22 16:09

Justin MacArthur