3c43a1cdd1
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
78 lines
2 KiB
TypeScript
78 lines
2 KiB
TypeScript
import type { Ref } from 'vue'
|
|
|
|
export function 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 }
|
|
}
|