refactor: use authenticateAndLoadUserInfo() instead of authenticate() and loadUserInfo() to reduce server calls
This commit is contained in:
parent
fa53502901
commit
43f5a43202
@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="topbar__container">
|
<div v-if="userInfo" class="topbar__container">
|
||||||
<div class="topbar__username">
|
<div class="topbar__username">
|
||||||
{{ userInfo.name }}
|
{{ userInfo.name }}
|
||||||
</div>
|
</div>
|
||||||
@ -25,7 +25,6 @@ const actionLogout = async () => {
|
|||||||
await useAuth().logout()
|
await useAuth().logout()
|
||||||
useRouter().push('/')
|
useRouter().push('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
|
@ -4,21 +4,14 @@ import { $fetch } from 'ohmyfetch'
|
|||||||
export interface useAuth {
|
export interface useAuth {
|
||||||
login(authCode: string): Promise<void>
|
login(authCode: string): Promise<void>
|
||||||
logout(): Promise<void>
|
logout(): Promise<void>
|
||||||
isAuthenticated(): Promise<boolean>
|
authenticateAndLoadUserInfo(): Promise<void>
|
||||||
loadUserInfo(): Promise<void>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (): useAuth => {
|
export default (): useAuth => {
|
||||||
const userInfoStore = useUserinfoStore()
|
const userInfoStore = useUserinfoStore()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isAuthenticated: async (): Promise<boolean> => {
|
authenticateAndLoadUserInfo: async (): Promise<void> => {
|
||||||
return $fetch('/api/userinfo')
|
|
||||||
.then(() => true)
|
|
||||||
.catch(() => false)
|
|
||||||
},
|
|
||||||
|
|
||||||
loadUserInfo: async (): Promise<void> => {
|
|
||||||
const userInfo = await $fetch('/api/userinfo')
|
const userInfo = await $fetch('/api/userinfo')
|
||||||
userInfoStore.setUserInfo(userInfo)
|
userInfoStore.setUserInfo(userInfo)
|
||||||
},
|
},
|
||||||
|
16
client/src/composables/useEngine.ts
Normal file
16
client/src/composables/useEngine.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
export interface useEngine {
|
||||||
|
start(): void
|
||||||
|
stop(): void
|
||||||
|
}
|
||||||
|
|
||||||
|
export default (): useEngine => {
|
||||||
|
return {
|
||||||
|
start: (): void => {
|
||||||
|
console.log('start engine')
|
||||||
|
},
|
||||||
|
|
||||||
|
stop: (): void => {
|
||||||
|
console.log('stop engine')
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
@ -24,8 +24,12 @@ import useAuth from '@/composables/useAuth'
|
|||||||
|
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
|
|
||||||
if (await useAuth().isAuthenticated()) {
|
try {
|
||||||
|
|
||||||
|
await useAuth().authenticateAndLoadUserInfo()
|
||||||
useRouter().push('/play')
|
useRouter().push('/play')
|
||||||
|
} catch (e) {
|
||||||
|
// nop
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="page-play__container">
|
<div class="page-play__container">
|
||||||
<TopBar />
|
<TopBar T/>
|
||||||
wohoo!
|
wohoo!
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -8,15 +8,20 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useRouter } from '#app'
|
import { useRouter } from '#app'
|
||||||
import useAuth from '@/composables/useAuth';
|
import useAuth from '@/composables/useAuth';
|
||||||
|
import useEngine from '@/composables/useEngine';
|
||||||
|
import { onMounted, onBeforeUnmount } from 'vue';
|
||||||
|
|
||||||
const { isAuthenticated, loadUserInfo } = useAuth()
|
const { authenticateAndLoadUserInfo } = useAuth()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
if (!await isAuthenticated()) {
|
try {
|
||||||
|
await authenticateAndLoadUserInfo()
|
||||||
|
} catch(e) {
|
||||||
router.push('/')
|
router.push('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
await loadUserInfo()
|
const { start: startEngine, stop: stopEngine } = useEngine()
|
||||||
|
onMounted(() => startEngine())
|
||||||
|
onBeforeUnmount(() => stopEngine())
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
|
Loading…
Reference in New Issue
Block a user