updated engine to fetch userinfo if not present

This commit is contained in:
Settel 2021-08-05 02:33:47 +02:00
parent 97b11d31d5
commit 5754b74fb2
3 changed files with 22 additions and 14 deletions

View File

@ -35,23 +35,20 @@
export default {
data() {
return {
user: {},
authCode: '',
errorMessage: '',
}
},
async fetch() {
try {
this.user = await this.$axios.$get('/api/userinfo')
this.$store.commit('engine/setUser', this.user)
} catch(e) {
// nop
}
this.$engine.fetchUserInfo()
},
computed: {
loginDisabled() {
return this.authCode.length < 6
},
user() {
return this.$store.state.engine.user
},
},
methods: {
async login(e) {

View File

@ -1,7 +1,7 @@
<template>
<div>
<pre class="json">{{ user }}</pre>
<pre class="json">{{ json }}</pre>
<pre class="json">{{ userJson }}</pre>
<pre class="json">{{ engineJson }}</pre>
</div>
</template>
@ -11,10 +11,10 @@ export default {
await this.$engine.start()
},
computed: {
json() {
engineJson() {
return JSON.stringify(this.$store.state.engine.json, null, 2)
},
user() {
userJson() {
return JSON.stringify(this.$store.state.engine.user, null, 2)
},
}

View File

@ -7,8 +7,10 @@ export default (context, inject) => {
lastFetched: [0, 0, 0, 0, 0],
async start() {
if (!store.state.engine.user) {
if (!await this.fetchUserInfo()) {
window.location.href = "/"
}
}
this.fetchUpdate()
},
async fetchUpdate() {
@ -21,8 +23,6 @@ export default (context, inject) => {
g: store.state.engine.user?.game,
},
})
console.log(`url: ${url}`)
const response = await $axios.get(url)
const json = response.data
store.commit('engine/setJson', json)
@ -48,6 +48,17 @@ export default (context, inject) => {
}, delay)
},
async fetchUserInfo() {
try {
const user = await $axios.$get('/api/userinfo')
store.commit('engine/setUser', user)
} catch(e) {
store.commit('engine/setUser', undefined)
return false
}
return true
},
}
inject('engine', engine)
}