diff --git a/client/src/components/LoginBox.vue b/client/src/components/LoginBox.vue index 53c2dc2..48574e4 100644 --- a/client/src/components/LoginBox.vue +++ b/client/src/components/LoginBox.vue @@ -13,9 +13,8 @@ import { useRouter } from '#app' import { ref, Ref } from 'vue' import useAuth from '@/composables/useAuth'; -// import { useMyselfStore } from '~~/src/stores/MyselfStore' -const { validate, setAuthCode } = useAuth() +const { authenticate, isAuthenticated } = useAuth() const router = useRouter() const vFocus = { mounted: (el: HTMLElement) => el.focus() } @@ -23,16 +22,14 @@ const vFocus = { mounted: (el: HTMLElement) => el.focus() } const authCode: Ref = ref('') const errorMessage: Ref = ref('') - -validate() - .then(() => router.push('/play')) - .catch(() => null) +if (await isAuthenticated()) { + router.push('/play') +} const login = (): void => { errorMessage.value = '' - setAuthCode(authCode.value) - validate().then(() => { + authenticate(authCode.value).then(() => { router.push('/play') }).catch(() => { errorMessage.value = 'login failed' diff --git a/client/src/composables/useAuth.ts b/client/src/composables/useAuth.ts index 447078b..d94d947 100644 --- a/client/src/composables/useAuth.ts +++ b/client/src/composables/useAuth.ts @@ -1,26 +1,25 @@ -import { useMyselfStore } from '~~/src/stores/MyselfStore' - export interface useAuth { - validate(): Promise - setAuthCode(authCode: string): void + authenticate(authCode: string): Promise + isAuthenticated(): Promise } export default () => { - const MyselfStore = useMyselfStore() - return { - validate: async (): Promise => { - if (MyselfStore.authCode.length != 6) { + isAuthenticated: async (): Promise => { + return $fetch('/api/userinfo') + .then(() => true) + .catch(() => false) + }, + + authenticate: async (authCode: string): Promise => { + if (authCode.length != 6) { throw Error('login failed') } - const resp = await $fetch(`/api/login?code=${MyselfStore.authCode}`) + const resp = await $fetch(`/api/login?code=${authCode}`) if (resp !== 'ok') { throw Error('login failed') } }, - setAuthCode: (authCode: string): void => { - MyselfStore.setAuthCode(authCode) - } } } diff --git a/client/src/stores/MyselfStore.ts b/client/src/stores/MyselfStore.ts deleted file mode 100644 index 193f43d..0000000 --- a/client/src/stores/MyselfStore.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { defineStore } from 'pinia' - -export const useMyselfStore = defineStore('MyselfStore', { - state: () => { - return { - authCode: '', - } - }, - actions: { - setAuthCode(authCode: string): void { - this.authCode = authCode - } - }, -}) diff --git a/client/src/stores/UserinfoStore.ts b/client/src/stores/UserinfoStore.ts new file mode 100644 index 0000000..d5c4773 --- /dev/null +++ b/client/src/stores/UserinfoStore.ts @@ -0,0 +1,8 @@ +import { defineStore } from 'pinia' + +export const useUserinfoStore = defineStore('UserinfoStore', { + state: () => { + return { + } + }, +})