From 8c21969246cd3ad2a8ef6942f5ea7c6064ac5d9a Mon Sep 17 00:00:00 2001 From: Settel Date: Fri, 29 Jul 2022 15:08:12 +0200 Subject: [PATCH] feat: store authCode in Pinia --- client/src/components/LoginBox.vue | 30 ++++++++++++++++++------------ client/src/composables/useAuth.ts | 26 ++++++++++++++++++++++++++ client/src/stores/MyselfStore.ts | 14 ++++++++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 client/src/composables/useAuth.ts create mode 100644 client/src/stores/MyselfStore.ts diff --git a/client/src/components/LoginBox.vue b/client/src/components/LoginBox.vue index 69a13be..53c2dc2 100644 --- a/client/src/components/LoginBox.vue +++ b/client/src/components/LoginBox.vue @@ -10,26 +10,32 @@ diff --git a/client/src/composables/useAuth.ts b/client/src/composables/useAuth.ts new file mode 100644 index 0000000..447078b --- /dev/null +++ b/client/src/composables/useAuth.ts @@ -0,0 +1,26 @@ +import { useMyselfStore } from '~~/src/stores/MyselfStore' + +export interface useAuth { + validate(): Promise + setAuthCode(authCode: string): void +} + + +export default () => { + const MyselfStore = useMyselfStore() + + return { + validate: async (): Promise => { + if (MyselfStore.authCode.length != 6) { + throw Error('login failed') + } + const resp = await $fetch(`/api/login?code=${MyselfStore.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 new file mode 100644 index 0000000..193f43d --- /dev/null +++ b/client/src/stores/MyselfStore.ts @@ -0,0 +1,14 @@ +import { defineStore } from 'pinia' + +export const useMyselfStore = defineStore('MyselfStore', { + state: () => { + return { + authCode: '', + } + }, + actions: { + setAuthCode(authCode: string): void { + this.authCode = authCode + } + }, +})