1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-09 18:39:58 +00:00
elk/composables/sign-in.ts

78 lines
2 KiB
TypeScript

import type { Ref } from 'vue'
export const useSignIn = (input?: Ref<HTMLInputElement | undefined>) => {
const singleInstanceServer = useRuntimeConfig().public.singleInstance
const userSettings = useUserSettings()
const users = useUsers()
const { t } = useI18n()
const busy = ref(false)
const error = ref(false)
const server = ref('')
const displayError = ref(false)
async function oauth() {
if (busy.value)
return
busy.value = true
error.value = false
displayError.value = false
await nextTick()
if (!singleInstanceServer && server.value)
server.value = server.value.split('/')[0]
try {
let href: string
if (singleInstanceServer) {
href = await (globalThis.$fetch as any)(`/api/${publicServer.value}/login`, {
method: 'POST',
body: {
force_login: users.value.length > 0,
origin: location.origin,
lang: userSettings.value.language,
},
})
busy.value = false
}
else {
href = await (globalThis.$fetch as any)(`/api/${server.value || publicServer.value}/login`, {
method: 'POST',
body: {
force_login: users.value.some(u => u.server === server.value),
origin: location.origin,
lang: userSettings.value.language,
},
})
}
location.href = href
}
catch (err) {
if (singleInstanceServer) {
console.error(err)
busy.value = false
await openErrorDialog({
title: t('common.error'),
messages: [t('error.sign_in_error')],
close: t('action.close'),
})
}
else {
displayError.value = true
error.value = true
await nextTick()
input?.value?.focus()
await nextTick()
setTimeout(() => {
busy.value = false
error.value = false
}, 512)
}
}
}
return { busy, displayError, error, server, singleInstanceServer, oauth }
}