<template>
  <router-view />
</template>

<script>
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'App',
  async created () {
    if (await this.loggedIn()) {
      this.$router.push('/dashboard')
    } else {
      this.$router.push('/')
    }

    this.$router.beforeEach(async (to, from, next) => {
      if (await this.loggedIn()) {
        if (to.path === '/') {
          this.$router.push('/dashboard')
        } else {
          next()
        }
      } else {
        console.log('logged out')
        if (to.path !== '/') {
          this.$router.push('/')
        } else {
          next()
        }
      }
    })
  },
  methods: {
    async loggedIn () {
      let creds = await this.$helpers.getStoredCredentials()
      let logged = await this.$helpers.verifyCredentials(creds)
      return logged
    }
  }
})
</script>
