Skip to content

Commit

Permalink
Merge pull request #83 from ynufes-tech/shion/fix-71
Browse files Browse the repository at this point in the history
composables-User情報管理体制整理 | ApiClientの修正
  • Loading branch information
Shion1305 authored May 12, 2024
2 parents 024ba00 + 1592260 commit 27082d7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 66 deletions.
60 changes: 32 additions & 28 deletions composables/useApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -49,29 +36,46 @@ 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)
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: 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<BackendResponse> => {
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 }
}

Expand Down
43 changes: 37 additions & 6 deletions composables/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
Expand All @@ -22,7 +32,7 @@ export const useLogin = () => {
const authStore = useAuthStore()
authStore.setToken(token.token)
} catch (e) {
console.error(e)
console.error('error in authWithCode', e)
return false
}
return true
Expand All @@ -38,20 +48,17 @@ export const useLogin = () => {
const user = resp.data as User
authStore.setUser(user)
} catch (e) {
console.error(e)
console.error('error in trySignIn', e)
return false
}
return true
}
const getCurrentUser = async (): Promise<User | null> => {
if (authStore.getUser) {
console.log('authStore.getUser is exist', authStore.getUser)
return authStore.getUser
}
if (authStore.getToken) {
console.log('authStore.getUser is not exist', authStore.getUser)
await trySignIn()
console.log('after trySignin', authStore.getUser)
return authStore.getUser
}
return null
Expand All @@ -60,5 +67,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<boolean> => {
try {
const resp = await client.post('/api/v1/user/info', ui)
if (resp.error) {
return false
}
authStore.clearUser()
return true
} catch (err) {
console.error('error in updateUserInfo', err)
return false
}
}

return {
getCurrentUser,
signOut,
trySignIn,
authWithCode,
updateUserInfo
}
}
30 changes: 0 additions & 30 deletions composables/useUserInfo.ts

This file was deleted.

3 changes: 1 addition & 2 deletions pages/welcome.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts" setup>
import { useLogin } from '~/composables/useLogin'
import { useUserInfo } from '~/composables/useUserInfo'
useHead({ title: 'welcome' })
definePageMeta({
layout: false
Expand All @@ -27,7 +26,7 @@ const submit = async () => {
gender: Number(gender.value),
student_id: studentID.value as string
}
await useUserInfo()
await useLogin()
.updateUserInfo(userInfo)
.then(() => {
console.log('success')
Expand Down

0 comments on commit 27082d7

Please sign in to comment.