diff --git a/ui/src/accounts/AccountEntry.vue b/ui/src/accounts/AccountEntry.vue index 23a1cdf5d..a3b8126f8 100644 --- a/ui/src/accounts/AccountEntry.vue +++ b/ui/src/accounts/AccountEntry.vue @@ -9,54 +9,129 @@ import { Input } from "@/components/ui/input"; import { api, describeError } from "@/lib/api"; import { errors } from "@/copy/errors"; import { passkeysAvailable, useAccountsStore, usePasskey } from "./accounts"; -const accounts=useAccountsStore(); -const step=ref(1);const recovery=ref(false);const recoveryCode=ref(""); -const organisation=ref("");const workspace=ref("Default");const publicURL=ref(window.location.origin); -const name=ref("");const email=ref("");const password=ref("");const busy=ref(false);const error=ref("");const notice=ref(""); -const setup=computed(()=>accounts.status?.setup_required===true); -const passkey=computed(()=>passkeysAvailable(accounts.status?.public_url??"")); -async function submit(){ - if(setup.value&&step.value===1&&!recovery.value){step.value=2;return} - busy.value=true;error.value="";notice.value=""; - try{ - if(recovery.value){await api.recoverAccount(email.value,recoveryCode.value,password.value);recovery.value=false;password.value="";notice.value="Password changed.";return} - if(setup.value)await api.setupAccount({organisation_name:organisation.value,workspace_name:workspace.value,public_url:publicURL.value.replace(/\/$/,""),display_name:name.value,email:email.value,password:password.value}); - else await api.signIn(email.value,password.value); - password.value=""; - window.location.assign(setup.value?"/account?welcome=1":new URLSearchParams(window.location.search).has("reauth")?"/account":"/"); - }catch(cause){error.value=describeError(cause, errors.common.unknown);await accounts.load()} - finally{busy.value=false} + +const accounts = useAccountsStore(); +const recovery = ref(false); +const recoveryCode = ref(""); +const organisation = ref(""); +const workspace = ref("Default"); +const name = ref(""); +const email = ref(""); +const password = ref(""); +const busy = ref(false); +const error = ref(""); +const notice = ref(""); +const setup = computed(() => accounts.status?.setup_required === true); +const passkey = computed(() => passkeysAvailable(accounts.status?.public_url ?? "")); + +// Setup asks one thing at a time: the organisation, then its first workspace, +// then the account that owns it. +const ACCOUNT_STEP = 3; +const step = ref(1); + +async function submit() { + if (setup.value && !recovery.value && step.value < ACCOUNT_STEP) { + step.value += 1; + return; + } + busy.value = true; + error.value = ""; + notice.value = ""; + try { + if (recovery.value) { + await api.recoverAccount(email.value, recoveryCode.value, password.value); + recovery.value = false; + password.value = ""; + notice.value = "Password changed."; + return; + } + if (setup.value) { + // The instance URL is the address the operator already reached, and it + // stays editable under Settings, authentication once passkeys exist. + await api.setupAccount({ + organisation_name: organisation.value, + workspace_name: workspace.value, + public_url: window.location.origin, + display_name: name.value, + email: email.value, + password: password.value, + }); + } else { + await api.signIn(email.value, password.value); + } + password.value = ""; + window.location.assign( + setup.value + ? "/account?welcome=1" + : new URLSearchParams(window.location.search).has("reauth") + ? "/account" + : "/", + ); + } catch (cause) { + error.value = describeError(cause, errors.common.unknown); + await accounts.load(); + } finally { + busy.value = false; + } +} + +async function signInPasskey() { + busy.value = true; + error.value = ""; + try { + await usePasskey(false); + window.location.assign("/"); + } catch (cause) { + error.value = describeError(cause, errors.common.unknown); + } finally { + busy.value = false; + } } -async function signInPasskey(){busy.value=true;error.value="";try{await usePasskey(false);window.location.assign("/")}catch(cause){error.value=describeError(cause, errors.common.unknown)}finally{busy.value=false}} +