knowyt/client/src/composables/useAuth.ts

26 lines
591 B
TypeScript
Raw Normal View History

2022-07-29 13:08:12 +00:00
export interface useAuth {
authenticate(authCode: string): Promise<void>
isAuthenticated(): Promise<boolean>
2022-07-29 13:08:12 +00:00
}
export default () => {
return {
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')
}
const resp = await $fetch(`/api/login?code=${authCode}`)
2022-07-29 13:08:12 +00:00
if (resp !== 'ok') {
throw Error('login failed')
}
},
}
}