diff --git a/src/router/index.ts b/src/router/index.ts index 0cd8220..7dd9727 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,6 +1,7 @@ import { createRouter, createWebHistory } from 'vue-router' import HomeView from '@/views/HomeView.vue' import UserDashboard from '@/views/UserDashboard.vue' +import { isLoggedIn } from '@/ts/authorization' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -9,13 +10,37 @@ const router = createRouter({ path: '/', name: 'home', component: HomeView, + beforeEnter: (to) => { + isLoggedIn().then((response) => { + if (response) { + const redirect = to.query.redirect?.toString() + if (redirect != undefined) { + router.replace(redirect) + } else { + router.replace('/dashboard') + } + } + }) + }, + meta: { requiresAuth: false }, }, { path: '/dashboard', name: 'user-dashboard', component: UserDashboard, + meta: { requiresAuth: true }, }, ], }) +router.beforeEach((to) => { + if (to.meta.requiresAuth) { + isLoggedIn().then((response) => { + if (response == false) { + router.replace('/?redirect=' + to.fullPath) + } + }) + } +}) + export default router diff --git a/src/ts/authorization.ts b/src/ts/authorization.ts new file mode 100644 index 0000000..e50562e --- /dev/null +++ b/src/ts/authorization.ts @@ -0,0 +1,15 @@ +export function isLoggedIn(): Promise { + return fetch('http://localhost:31337/authorized', { + credentials: 'include', + }) + .then((res) => { + return res.json().then((res_json) => { + const is_authorized = res_json.message + console.log('whwhwhw' + is_authorized) + return Boolean(is_authorized) + }) + }) + .catch(() => { + return false + }) +} diff --git a/src/views/UserDashboard.vue b/src/views/UserDashboard.vue index 43bccf1..a6fc8dc 100644 --- a/src/views/UserDashboard.vue +++ b/src/views/UserDashboard.vue @@ -26,7 +26,7 @@ onMounted(update_info)

Logout