68 lines
1.9 KiB
Vue
68 lines
1.9 KiB
Vue
<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>
|