Added frontend set_token

This commit is contained in:
Mutzi 2022-08-24 21:17:35 +02:00
parent afd7709fa8
commit 70dd272b5d
3 changed files with 27 additions and 10 deletions

View File

@ -1,11 +1,11 @@
<script setup async lang="ts">
import { provide, ref } from 'vue';
import { useRouter } from 'vue-router';
import { Auth, TokenInjectType, isErrorResponse } from '@/api';
import { TokenInjectType } from '@/api';
const router = useRouter();
const jwt = ref<string | null>(null);
const jwt = ref<string | null>(localStorage.getItem('token'));
function setToken(token: string) {
jwt.value = token;
@ -18,14 +18,6 @@ function logout() {
router.push({ name: 'login' });
}
jwt.value = localStorage.getItem('token');
if (jwt.value == null) await router.push({ name: 'login' });
else {
const new_token = await Auth.refresh_token(jwt.value ?? '');
if (isErrorResponse(new_token)) logout();
else setToken(new_token.jwt);
}
provide<TokenInjectType>('jwt', {
jwt,
setToken,

View File

@ -4,10 +4,12 @@ import SignupView from '@/views/SignupView.vue';
import HomeView from '@/views/HomeView.vue';
import AboutView from '@/views/AboutView.vue';
import FSView from '@/views/FSView.vue';
import SetTokenView from '@/views/SetTokenView.vue';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'home',
component: HomeView
},
{
@ -25,6 +27,10 @@ const routes: Array<RouteRecordRaw> = [
name: 'signup',
component: SignupView
},
{
path: '/set_token',
component: SetTokenView
},
{
path: '/fs/:node_id',
name: 'fs',

View File

@ -0,0 +1,19 @@
<script setup lang="ts">
import { inject } from 'vue';
import { TokenInjectType } from '@/api';
import { useRoute, useRouter } from 'vue-router';
const router = useRouter();
const route = useRoute();
const jwt = inject<TokenInjectType>('jwt') as TokenInjectType;
if ('token' in route.query) jwt.setToken(route.query['token'] as string);
router.replace({ path: '/' });
</script>
<template>
<router-link to="home">Click here to go home</router-link>
</template>
<style scoped></style>