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

    <p>user logged in: {{ user }}</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: 'UserCP',

  components: {
    Header
  },

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

  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.user = response.data.message
        } else {
          this.goHome()
        }
      })
  }
}
</script>
