import { createRouter, createWebHistory } from 'vue-router'
import unifiedLoginSDK from 'oauth2-login-sdk';
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      redirect: '/home'
    },
    {
      path: '/',
      name: 'home',
      component: () => import('../components/Home.vue')
    }
  ]
})
router.beforeEach(async (to, _from, next) => {
  debugger
  if (!unifiedLoginSDK.isAuthenticated()) {
    if (to.path === '/oauth2/callback') {
      await unifiedLoginSDK.handleCallback()
    }else{
      await unifiedLoginSDK.login()
    }
  } else {
    next()
  }
})

export default router