fileserver/frontend/src/App.vue

128 lines
2.2 KiB
Vue
Raw Normal View History

2022-08-17 19:59:51 +00:00
<script setup async lang="ts">
2022-09-03 21:32:20 +00:00
import type { MenuOption } from 'naive-ui';
import { provide, ref, h } from 'vue';
import { useRouter, RouterLink } from 'vue-router';
import type { TokenInjectType } from '@/api';
import { useMessage, NMenu, NPageHeader, NIcon } from 'naive-ui';
import { BareMetalServer02 } from '@vicons/carbon';
import { is_admin } from '@/api/admin';
2022-08-17 19:59:51 +00:00
const router = useRouter();
2022-09-03 21:32:20 +00:00
const message = useMessage();
2022-08-17 19:59:51 +00:00
2022-09-03 21:32:20 +00:00
const jwt = ref<string | null>(localStorage.getItem('token'));
2022-08-17 19:59:51 +00:00
function setToken(token: string) {
2022-09-03 21:32:20 +00:00
jwt.value = token;
localStorage.setItem('token', token);
2022-08-17 19:59:51 +00:00
}
function logout() {
2022-09-03 21:32:20 +00:00
jwt.value = null;
localStorage.removeItem('token');
router.push({ name: 'login' });
2022-08-17 19:59:51 +00:00
}
2022-09-03 21:32:20 +00:00
provide<TokenInjectType>('jwt', {
jwt,
setToken,
logout
2022-08-17 19:59:51 +00:00
});
2022-09-03 21:32:20 +00:00
router.afterEach(() => message.destroyAll());
function handleUpdateValue(key: string) {
if (key === 'login') logout();
}
const menuOptions: MenuOption[] = [
{
label: () =>
h(
RouterLink,
{
to: '/'
},
{ default: () => 'Files' }
),
key: 'fs'
},
{
label: () =>
h(
RouterLink,
{
to: '/profile'
},
{ default: () => 'Profile' }
),
key: 'profile'
},
{
label: () =>
h(
RouterLink,
{
to: '/login'
},
{ default: () => 'Logout' }
),
key: 'login'
}
];
if (await is_admin(jwt.value ?? ''))
menuOptions.unshift({
label: () =>
h(
RouterLink,
{
to: '/admin'
},
{ default: () => 'Admin' }
),
key: 'admin'
});
2022-08-17 19:59:51 +00:00
</script>
<template>
2022-09-03 21:32:20 +00:00
<n-page-header style="margin-bottom: 3em">
<template #title>
<n-icon class="nav-icon" size="1.5em">
<BareMetalServer02 />
</n-icon>
MFileserver
</template>
<template #extra>
<n-menu
v-if="jwt != null"
mode="horizontal"
:options="menuOptions"
@update:value="handleUpdateValue"
/>
</template>
</n-page-header>
<router-view />
2022-08-17 19:59:51 +00:00
</template>
<style lang="scss">
2022-09-03 21:32:20 +00:00
body {
height: 100%;
padding: 2em;
display: flex;
justify-content: center;
align-content: center;
2022-08-17 19:59:51 +00:00
}
2022-09-03 21:32:20 +00:00
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
}
2022-08-17 19:59:51 +00:00
2022-09-03 21:32:20 +00:00
.nav-icon {
top: 0.25em;
2022-08-17 19:59:51 +00:00
}
</style>