Yay let's go we have a functioning frontend
This commit is contained in:
parent
d8e7e34b83
commit
426542d4c9
3 changed files with 41 additions and 1 deletions
|
@ -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
|
||||
|
|
15
src/ts/authorization.ts
Normal file
15
src/ts/authorization.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
export function isLoggedIn(): Promise<boolean> {
|
||||
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
|
||||
})
|
||||
}
|
|
@ -26,7 +26,7 @@ onMounted(update_info)
|
|||
</div>
|
||||
<a
|
||||
class="flex w-full p-2 font-mono text-lg md:p-4 md:text-2xl bg-secondary ring-2 ring-secondary/80 h-fit0 hover:bg-secondary/50"
|
||||
href="logout"
|
||||
href="http://localhost:31337/logout"
|
||||
>
|
||||
<div class="w-full h-full text-center bg-primary">
|
||||
<p class="drop-shadow-accent drop-shadow-md">Logout</p>
|
||||
|
|
Loading…
Reference in a new issue