2022-08-17 19:59:51 +00:00
|
|
|
<script setup async lang="ts">
|
|
|
|
import { provide, ref } from 'vue';
|
|
|
|
import { useRouter } from 'vue-router';
|
2022-08-24 19:17:35 +00:00
|
|
|
import { TokenInjectType } from '@/api';
|
2022-08-17 19:59:51 +00:00
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
2022-08-24 19:17:35 +00:00
|
|
|
const jwt = ref<string | null>(localStorage.getItem('token'));
|
2022-08-17 19:59:51 +00:00
|
|
|
|
|
|
|
function setToken(token: string) {
|
|
|
|
jwt.value = token;
|
|
|
|
localStorage.setItem('token', token);
|
|
|
|
}
|
|
|
|
|
|
|
|
function logout() {
|
|
|
|
jwt.value = null;
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
router.push({ name: 'login' });
|
|
|
|
}
|
|
|
|
|
|
|
|
provide<TokenInjectType>('jwt', {
|
|
|
|
jwt,
|
|
|
|
setToken,
|
|
|
|
logout
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<nav>
|
2022-08-25 15:54:00 +00:00
|
|
|
<template v-if="jwt != null">
|
|
|
|
<router-link to="/">Files</router-link>
|
|
|
|
<span style="margin-left: 2em" />
|
|
|
|
<router-link to="/profile">Profile</router-link>
|
|
|
|
<span style="margin-left: 2em" />
|
|
|
|
<router-link to="/login" @click="logout()">Logout</router-link>
|
|
|
|
</template>
|
2022-08-17 19:59:51 +00:00
|
|
|
</nav>
|
|
|
|
<router-view />
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
#app {
|
|
|
|
font-family: Avenir, Helvetica, Arial, sans-serif;
|
|
|
|
-webkit-font-smoothing: antialiased;
|
|
|
|
-moz-osx-font-smoothing: grayscale;
|
|
|
|
text-align: center;
|
|
|
|
color: #2c3e50;
|
|
|
|
}
|
|
|
|
|
|
|
|
nav {
|
|
|
|
padding: 30px;
|
|
|
|
|
|
|
|
a {
|
|
|
|
font-weight: bold;
|
|
|
|
color: #2c3e50;
|
|
|
|
|
|
|
|
&.router-link-exact-active {
|
|
|
|
color: #42b983;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|