Add login page

This commit is contained in:
Daniel_I_Am 2021-09-02 12:40:34 +02:00
parent a022c4dbfc
commit 77deb525df
2 changed files with 69 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import Blog from './views/Blog.vue';
import BlogArticle from './views/BlogArticle.vue'; import BlogArticle from './views/BlogArticle.vue';
import Snippets from './views/Snippets.vue'; import Snippets from './views/Snippets.vue';
import Contact from './views/Contact.vue'; import Contact from './views/Contact.vue';
import Login from './views/Login.vue';
import NotFound from './views/NotFound.vue'; import NotFound from './views/NotFound.vue';
@ -14,6 +15,7 @@ const routes = [
{ path: '/blog/:year/:month/:day/:id-:slug', name: 'blog-article', component: BlogArticle, props: true }, { path: '/blog/:year/:month/:day/:id-:slug', name: 'blog-article', component: BlogArticle, props: true },
{ path: '/snippets', name: 'snippets', component: Snippets }, { path: '/snippets', name: 'snippets', component: Snippets },
{ path: '/contact', name: 'contact', component: Contact }, { path: '/contact', name: 'contact', component: Contact },
{ path: '/login', name: 'login', component: Login },
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }, { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
]; ];

View File

@ -0,0 +1,67 @@
<template>
<page-header></page-header>
<container>
<p>
<span v-if="!!tokenId">Authenticated with token ID {{ tokenId }}</span><br/>
Authentication status: {{ status ? 'Valid' : 'Invalid' }}
<button @click="checkValidity">Refresh</button>
</p>
</container>
</template>
<script>
import PageHeader from "../components/PageHeader.vue";
import Container from "../components/Container.vue";
export default {
components: {
PageHeader,
Container,
},
data() {
return {
status: false,
tokenId: null
};
},
mounted() {
if (!this.getCurrentToken()) {
this.requestNewToken();
} else {
this.checkValidity();
}
},
methods: {
requestNewToken() {
axios.post('/api/auth')
.then(res => {
window.localStorage.setItem('authentication-token', JSON.stringify(res.data));
axios.defaults.headers.common.Authorization = res.data.token;
this.tokenId = res.data.id;
});
},
checkValidity() {
axios.get('/api/auth')
.then(res => {
this.status = res.data.status;
});
},
getCurrentToken() {
if (!!axios.defaults.headers.common.Authorization) {
return axios.defaults.headers.common.Authorization;
}
if (window.localStorage.getItem('authentication-token')) {
const tokenData = JSON.parse(window.localStorage.getItem('authentication-token'));
if (moment(tokenData.valid_until) > moment()) {
this.tokenId = tokenData.id;
axios.defaults.headers.common.Authorization = tokenData.token;
return tokenData.token;
}
}
}
}
}
</script>