From 81041bbce2018a580d238345a40fc5afec741bec Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sun, 5 May 2024 22:15:01 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20(composables/useApiCli?= =?UTF-8?q?ent)improve=20response=20data=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/useApiClient.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/composables/useApiClient.ts b/composables/useApiClient.ts index 7a34559..de15816 100644 --- a/composables/useApiClient.ts +++ b/composables/useApiClient.ts @@ -49,18 +49,26 @@ const useApiClient = () => { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}) } + if (typeof body !== 'string') body = JSON.stringify(body) return await fetch(`${nuxtConfig.public.baseURL}${url}`, { method: 'POST', headers, body }) .then((resp: any) => { - if (resp.ok) + if (resp.ok) { + let d + try { + d = resp.json() + } catch (e) { + d = null + } return { - data: resp.json(), + data: d, unauthorized: false, error: null } as BackendResponse + } if (resp.status === 401) { authStore.clearToken() authStore.clearUser() From f8b9b5c9cad89e7c3711693b87ec7b3a20e0db43 Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sun, 5 May 2024 22:18:28 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(composables/useApiCli?= =?UTF-8?q?ent)=20response=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/useApiClient.ts | 66 +++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/composables/useApiClient.ts b/composables/useApiClient.ts index de15816..12ba8f1 100644 --- a/composables/useApiClient.ts +++ b/composables/useApiClient.ts @@ -24,20 +24,7 @@ const useApiClient = () => { `${nuxtConfig.public.baseURL}${url}?${new URLSearchParams(params)}`, { headers } ) - .then((resp: any) => { - if (resp.ok) - return { - data: resp.json(), - unauthorized: false, - error: null - } as BackendResponse - if (resp.status === 401) { - authStore.clearToken() - authStore.clearUser() - return { data: null, unauthorized: true, error: resp.status } - } - return { data: null, unauthorized: false, error: resp.statusText } - }) + .then((resp: Response) => handleResponse(resp)) .catch((e: any) => { return { data: null, unauthorized: false, error: e } as BackendResponse }) @@ -55,31 +42,40 @@ const useApiClient = () => { headers, body }) - .then((resp: any) => { - if (resp.ok) { - let d - try { - d = resp.json() - } catch (e) { - d = null - } - return { - data: d, - unauthorized: false, - error: null - } as BackendResponse - } - if (resp.status === 401) { - authStore.clearToken() - authStore.clearUser() - return { data: null, unauthorized: true, error: resp.status } - } - return { data: null, unauthorized: true, error: resp.statusText } - }) + .then((resp: any) => handleResponse(resp)) .catch((e: any) => { return { data: null, unauthorized: false, error: e } as BackendResponse }) } + + const handleResponse = async (resp: Response): Promise => { + if (resp.ok) { + const respStr = await resp.text() + let respData + try { + respData = JSON.parse(respStr) + } catch (e) {} + return { + data: respData, + unauthorized: false, + error: null + } as BackendResponse + } + if (resp.status === 401) { + authStore.clearToken() + authStore.clearUser() + return { + data: null, + unauthorized: true, + error: resp.status + } as BackendResponse + } + return { + data: null, + unauthorized: true, + error: resp.statusText + } as BackendResponse + } return { get, post } } From 17b7095c5ccf73122b9a18a6754edfae1fb9784d Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Mon, 6 May 2024 01:15:09 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9B=20demolish=20useUserInfo,=20un?= =?UTF-8?q?ify=20userdata=20management?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/useLogin.ts | 36 +++++++++++++++++++++++++++++++++++- composables/useUserInfo.ts | 30 ------------------------------ pages/welcome.vue | 3 +-- 3 files changed, 36 insertions(+), 33 deletions(-) delete mode 100644 composables/useUserInfo.ts diff --git a/composables/useLogin.ts b/composables/useLogin.ts index 0cbbcae..f23e952 100644 --- a/composables/useLogin.ts +++ b/composables/useLogin.ts @@ -2,6 +2,16 @@ import useApiClient from '~/composables/useApiClient' import { useAuthStore, User } from '~/stores/auth' +export type UserInfo = { + name_first: string + name_first_kana: string + name_last: string + name_last_kana: string + email: string + gender: number + student_id: string +} + export const useLogin = () => { const authStore = useAuthStore() const authWithCode = async (code: string): Promise => { @@ -60,5 +70,29 @@ export const useLogin = () => { authStore.clearToken() authStore.clearUser() } - return { getCurrentUser, signOut, trySignIn, authWithCode } + + // this is client for using credentials + const client = useApiClient() + + const updateUserInfo = async (ui: UserInfo): Promise => { + try { + const resp = await client.post('/api/v1/user/info', ui) + if (resp.error) { + return false + } + authStore.clearUser() + return true + } catch (err) { + console.error(err) + return false + } + } + + return { + getCurrentUser, + signOut, + trySignIn, + authWithCode, + updateUserInfo + } } diff --git a/composables/useUserInfo.ts b/composables/useUserInfo.ts deleted file mode 100644 index 5d746b0..0000000 --- a/composables/useUserInfo.ts +++ /dev/null @@ -1,30 +0,0 @@ -// ユーザ情報登録や取得に関する処理 -import useApiClient from '~/composables/useApiClient' - -export type UserInfo = { - name_first: string - name_first_kana: string - name_last: string - name_last_kana: string - email: string - gender: number - student_id: string -} - -export const useUserInfo = () => { - const client = useApiClient() - const userInfo: Ref = useState('userInfo', () => null) - - const updateUserInfo = async (ui: UserInfo): Promise => { - try { - console.log(ui) - const resp = await client.post('/api/v1/user/info', ui) - return !resp.error - } catch (err) { - console.error(err) - return false - } - } - - return { userInfo, updateUserInfo } -} diff --git a/pages/welcome.vue b/pages/welcome.vue index 5e862d5..c6240a1 100644 --- a/pages/welcome.vue +++ b/pages/welcome.vue @@ -1,6 +1,5 @@