2022-07-29 21:41:24 +00:00
|
|
|
import { useUserinfoStore } from "@/stores/UserinfoStore"
|
2022-07-31 20:56:53 +00:00
|
|
|
import { $fetch } from 'ohmyfetch'
|
2022-07-29 21:41:24 +00:00
|
|
|
|
2022-07-29 13:08:12 +00:00
|
|
|
export interface useAuth {
|
2022-07-29 19:11:09 +00:00
|
|
|
login(authCode: string): Promise<void>
|
|
|
|
logout(): Promise<void>
|
2022-07-31 21:19:56 +00:00
|
|
|
authenticateAndLoadUserInfo(): Promise<void>
|
2022-07-29 13:08:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 20:56:53 +00:00
|
|
|
export default (): useAuth => {
|
2022-07-29 21:41:24 +00:00
|
|
|
const userInfoStore = useUserinfoStore()
|
|
|
|
|
2022-07-29 13:08:12 +00:00
|
|
|
return {
|
2022-07-31 21:19:56 +00:00
|
|
|
authenticateAndLoadUserInfo: async (): Promise<void> => {
|
2022-07-29 21:41:24 +00:00
|
|
|
const userInfo = await $fetch('/api/userinfo')
|
|
|
|
userInfoStore.setUserInfo(userInfo)
|
|
|
|
},
|
|
|
|
|
2022-07-29 19:11:09 +00:00
|
|
|
login: async (authCode: string): Promise<void> => {
|
2022-07-29 13:31:36 +00:00
|
|
|
if (authCode.length != 6) {
|
2022-07-29 13:08:12 +00:00
|
|
|
throw Error('login failed')
|
|
|
|
}
|
2022-07-29 13:31:36 +00:00
|
|
|
const resp = await $fetch(`/api/login?code=${authCode}`)
|
2022-07-29 13:08:12 +00:00
|
|
|
if (resp !== 'ok') {
|
|
|
|
throw Error('login failed')
|
|
|
|
}
|
|
|
|
},
|
2022-07-29 19:11:09 +00:00
|
|
|
|
|
|
|
logout: async (): Promise<void> => {
|
|
|
|
await $fetch('/api/logout')
|
|
|
|
},
|
2022-07-29 13:08:12 +00:00
|
|
|
}
|
|
|
|
}
|