34 lines
854 B
TypeScript
34 lines
854 B
TypeScript
import { useUserinfoStore } from "@/stores/UserinfoStore"
|
|
import { $fetch } from 'ohmyfetch'
|
|
|
|
export interface useAuth {
|
|
login(authCode: string): Promise<void>
|
|
logout(): Promise<void>
|
|
authenticateAndLoadUserInfo(): Promise<void>
|
|
}
|
|
|
|
export default (): useAuth => {
|
|
const userInfoStore = useUserinfoStore()
|
|
|
|
return {
|
|
authenticateAndLoadUserInfo: async (): Promise<void> => {
|
|
const userInfo = await $fetch('/api/userinfo')
|
|
userInfoStore.setUserInfo(userInfo)
|
|
},
|
|
|
|
login: async (authCode: string): Promise<void> => {
|
|
if (authCode.length != 6) {
|
|
throw Error('login failed')
|
|
}
|
|
const resp = await $fetch(`/api/login?code=${authCode}`)
|
|
if (resp !== 'ok') {
|
|
throw Error('login failed')
|
|
}
|
|
},
|
|
|
|
logout: async (): Promise<void> => {
|
|
await $fetch('/api/logout')
|
|
},
|
|
}
|
|
}
|