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-29 13:31:36 +00:00
|
|
|
isAuthenticated(): Promise<boolean>
|
2022-07-29 13:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
return {
|
2022-07-29 13:31:36 +00:00
|
|
|
isAuthenticated: async (): Promise<boolean> => {
|
|
|
|
return $fetch('/api/userinfo')
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false)
|
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|