2022-07-29 13:08:12 +00:00
|
|
|
export interface useAuth {
|
2022-07-29 13:31:36 +00:00
|
|
|
authenticate(authCode: string): Promise<void>
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
|
|
|
|
authenticate: async (authCode: string): Promise<void> => {
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|