<template>
  <v-container>
    <Header />

    <p>user session: {{ sessionID }}</p>
    <v-btn @click.native="logout">Logout</v-btn>
  </v-container>
</template>

<script>
import * as Cookies from 'js-cookie'
import axios from '@/plugins/axios'

import Header from '@/components/Header.vue'

export default {
  name: 'About',

  components: {
    Header
  },

  data: () => ({
    sessionID: ''
  }),

  methods: {
    goHome() {
      Cookies.remove('session')
      this.$router.push({ name: 'home' })
    },

    logout() {
      axios
        .post('/user/logout', {
          session: Cookies.get('session')
        })
        .then(response => {
          if (response.data.success) {
            this.goHome()
          }
        })
    }
  },

  mounted() {
    axios
      .post('/user/login', {
        session: Cookies.get('session')
      })
      .then(response => {
        if (response.data.success) {
          this.sessionID = response.data.message
        } else {
          this.goHome()
        }
      })
  }
}
</script>
