Added user data loader
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2020-12-06 03:55:59 +01:00
parent f7b5dc41ca
commit 02af4b667e
4 changed files with 64 additions and 17 deletions

View File

@ -1,12 +1,39 @@
<template> <template>
<div id="app"> <div id="app">
<router-view/> <router-view/>
<md-snackbar md-position="center" :md-duration="4000" :md-active.sync="showWelcome">
<span>Welcome back {{ $store.state.auth.name }}!</span>
</md-snackbar>
</div> </div>
</template> </template>
<style> <script>
</style> import {mapState} from 'vuex';
export default {
name: "App",
data() {
return {
showWelcome: false
}
},
computed: mapState('auth', ['name']),
mounted() {
this.$store.dispatch("auth/loadUserData").catch(() => {
this.$router.push({name: "Welcome"});
})
},
watch: {
name(newValue, oldValue) {
if (oldValue === '') {
this.showWelcome = true;
}
}
}
}
</script>
<style> <style>
#app { #app {

View File

@ -19,7 +19,7 @@ import {
MdRipple, MdRipple,
MdDialog, MdDialog,
MdSpeedDial, MdSpeedDial,
MdDivider, MdSnackbar,
MdSubheader MdSubheader
} from 'vue-material/dist/components' } from 'vue-material/dist/components'
@ -42,7 +42,7 @@ Vue.use(MdAvatar);
Vue.use(MdRipple); Vue.use(MdRipple);
Vue.use(MdDialog); Vue.use(MdDialog);
Vue.use(MdSpeedDial); Vue.use(MdSpeedDial);
Vue.use(MdDivider); Vue.use(MdSnackbar);
Vue.use(MdSubheader); Vue.use(MdSubheader);
Vue.prototype.$api = axios.create({ Vue.prototype.$api = axios.create({

View File

@ -31,6 +31,7 @@ export default {
state.name = ''; state.name = '';
}, },
logout(state) { logout(state) {
state.processing = false;
state.token = ''; state.token = '';
state.name = ''; state.name = '';
} }
@ -38,7 +39,7 @@ export default {
actions: { actions: {
performLogin({commit}, creds) { performLogin({commit}, creds) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
commit('auth_started') commit('auth_started');
axios.post("auth/login", creds, {baseURL}).then(resp => { axios.post("auth/login", creds, {baseURL}).then(resp => {
const token = resp.data.token; const token = resp.data.token;
@ -46,12 +47,12 @@ export default {
return reject(); return reject();
} }
localStorage.setItem(JWT_KEY_NAME, token) localStorage.setItem(JWT_KEY_NAME, token);
commit('auth_success', {token, name: creds.name}) commit('auth_success', {token, name: creds.name});
return resolve(resp); return resolve(resp);
}).catch(err => { }).catch(err => {
commit('auth_fail') commit('auth_fail');
localStorage.removeItem(JWT_KEY_NAME) localStorage.removeItem(JWT_KEY_NAME);
return reject(err); return reject(err);
}) })
}) })
@ -66,13 +67,12 @@ export default {
return reject(); return reject();
} }
localStorage.setItem(JWT_KEY_NAME, token);
localStorage.setItem(JWT_KEY_NAME, token) commit('auth_success', {token, name: creds.name});
commit('auth_success', {token, name: creds.name})
resolve(resp) resolve(resp)
}).catch(err => { }).catch(err => {
commit('auth_fail') commit('auth_fail');
localStorage.removeItem(JWT_KEY_NAME) localStorage.removeItem(JWT_KEY_NAME);
reject(err) reject(err)
}) })
}) })
@ -83,6 +83,27 @@ export default {
commit('logout') commit('logout')
resolve(); resolve();
}); });
},
loadUserData({commit, state}) {
return new Promise((resolve, reject) => {
if (state.token && !state.name) {
commit('auth_started');
axios.get('auth/me', {
baseURL,
headers: {"Authorization": "Bearer " + state.token}
}).then((data) => {
commit("auth_success", {
token: state.token,
name: data.data.name
});
return resolve();
}).catch(() => {
commit("auth_fail");
localStorage.removeItem(JWT_KEY_NAME);
return reject();
});
}
});
} }
}, },
modules: {}, modules: {},

View File

@ -137,13 +137,12 @@ export default {
}, },
// --- end of magic hack // --- end of magic hack
handleNodeMove(id) { handleNodeMove(id) {
console.log(id);
const moved_node = this.model.nodes.find((node) => node.id === id); const moved_node = this.model.nodes.find((node) => node.id === id);
console.log(moved_node);
if ((!moved_node) || (!moved_node.apiId)) { if ((!moved_node) || (!moved_node.apiId)) {
return; return;
} }
console.log("Anyád")
this.pendingNodeMoves++; this.pendingNodeMoves++;
this.$api.put(`objects/streamerobjects/coordmodify/${moved_node.apiId}`, { this.$api.put(`objects/streamerobjects/coordmodify/${moved_node.apiId}`, {
x: moved_node.x, x: moved_node.x,