feat(ui/accounts): ask for setup one thing at a time

Organisation, first workspace and the admin account are separate steps. The
instance URL is no longer asked for: it is the address the operator already
reached, and Settings, authentication already edits it.
This commit is contained in:
sam committed 2026-11-20 19:32:24 +11:00
1 parent cf6108ad14
commit 904c510246
1 file changed
+122 -47
+122 -47
View File
@@ -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}}
</script>
<template>
<main class="mx-auto flex min-h-[90dvh] max-w-md flex-col justify-center px-5 py-10">
<Card class="gap-0 py-6 px-6 shadow-sm">
<div class="mb-2"><Logo /></div>
<h1 class="text-title font-medium">{{setup?(step===1?'Set up your organisation':'Create your account'):recovery?'Recover your account':'Sign in'}}</h1>
<form class="mt-6 space-y-4" @submit.prevent="submit">
<template v-if="setup&&step===1">
<Label class="block space-y-1.5 text-label" for="organisation">Organisation name<Input id="organisation" v-model="organisation" autocomplete="organization" required maxlength="200" /></Label>
<Label class="block space-y-1.5 text-label" for="workspace">First workspace<Input id="workspace" v-model="workspace" required maxlength="200" /></Label>
<Label class="block space-y-1.5 text-label" for="public-url">Instance URL<Input id="public-url" v-model="publicURL" type="url" required autocomplete="url" /></Label>
<p class="text-caption text-fg-muted">Passkeys require HTTPS and a domain name, or localhost.</p>
</template>
<template v-else>
<Label v-if="setup" class="block space-y-1.5 text-label" for="account-name">Your name<Input id="account-name" v-model="name" autocomplete="name" required maxlength="200" /></Label>
<Label class="block space-y-1.5 text-label" for="account-email">Email<Input id="account-email" v-model="email" type="email" autocomplete="username" :spellcheck="false" required /></Label>
<Label v-if="recovery" class="block space-y-1.5 text-label" for="recovery-code">Recovery code<Input id="recovery-code" v-model="recoveryCode" autocomplete="off" required /></Label>
<Label class="block space-y-1.5 text-label" for="account-password">{{recovery?"New password":"Password"}}<Input id="account-password" v-model="password" type="password" :autocomplete="setup||recovery?'new-password':'current-password'" :minlength="setup||recovery?15:undefined" maxlength="1024" required /></Label>
<p v-if="setup" class="text-caption text-fg-muted">At least 15 characters.</p>
</template>
<Alert v-if="error" variant="destructive"><AlertDescription>{{error}}</AlertDescription></Alert>
<Alert v-if="notice" class="mt-4"><AlertDescription>{{notice}}</AlertDescription></Alert>
<div class="flex gap-2"><Button v-if="setup&&step===2" type="button" variant="outline" :disabled="busy" @click="step=1">Back</Button><Button type="submit" class="flex-1" :disabled="busy">{{busy?'Please wait…':setup?(step===1?'Continue':'Create account'):recovery?'Reset password':'Sign in'}}</Button></div>
</form>
<div v-if="!setup&&!recovery" class="mt-4 flex flex-col gap-2">
<Button v-if="passkey" variant="outline" :disabled="busy" @click="signInPasskey">Sign in with a passkey</Button>
<Button v-if="accounts.status?.oidc" variant="outline" as-child><a href="/oauth2/login">Sign in with OIDC</a></Button>
</div>
<Button v-if="!setup" variant="link" class="mt-3" :disabled="busy" @click="recovery=!recovery;error=''">{{recovery?'Back to sign in':'Use a recovery code'}}</Button>
</Card>
</main>
<main class="mx-auto flex min-h-[90dvh] max-w-md flex-col justify-center px-5 py-10">
<Card class="gap-0 py-6 px-6 shadow-sm">
<div class="mb-2"><Logo /></div>
<h1 class="text-title font-medium">
{{
setup
? step === 1
? "Set up your organisation"
: step === 2
? "Create your first workspace"
: "Create your account"
: recovery
? "Recover your account"
: "Sign in"
}}
</h1>
<form class="mt-6 space-y-4" @submit.prevent="submit">
<template v-if="setup && step === 1">
<Label class="block space-y-1.5 text-label" for="organisation">Name<Input id="organisation" v-model="organisation" autocomplete="organization" required maxlength="200" /></Label>
</template>
<template v-else-if="setup && step === 2">
<Label class="block space-y-1.5 text-label" for="workspace">Name<Input id="workspace" v-model="workspace" required maxlength="200" /></Label>
</template>
<template v-else>
<Label v-if="setup" class="block space-y-1.5 text-label" for="account-name">Your name<Input id="account-name" v-model="name" autocomplete="name" required maxlength="200" /></Label>
<Label class="block space-y-1.5 text-label" for="account-email">Email<Input id="account-email" v-model="email" type="email" autocomplete="username" :spellcheck="false" required /></Label>
<Label v-if="recovery" class="block space-y-1.5 text-label" for="recovery-code">Recovery code<Input id="recovery-code" v-model="recoveryCode" autocomplete="off" required /></Label>
<Label class="block space-y-1.5 text-label" for="account-password">{{ recovery ? "New password" : "Password" }}<Input id="account-password" v-model="password" type="password" :autocomplete="setup || recovery ? 'new-password' : 'current-password'" :minlength="setup || recovery ? 15 : undefined" maxlength="1024" required /></Label>
<p v-if="setup" class="text-caption text-fg-muted">At least 15 characters.</p>
</template>
<Alert v-if="error" variant="destructive"><AlertDescription>{{ error }}</AlertDescription></Alert>
<Alert v-if="notice" class="mt-4"><AlertDescription>{{ notice }}</AlertDescription></Alert>
<div class="flex gap-2">
<Button v-if="setup && step > 1" type="button" variant="outline" :disabled="busy" @click="step -= 1">Back</Button>
<Button type="submit" class="flex-1" :disabled="busy">{{ busy ? "Please wait…" : setup ? (step === ACCOUNT_STEP ? "Create account" : "Continue") : recovery ? "Reset password" : "Sign in" }}</Button>
</div>
</form>
<div v-if="!setup && !recovery" class="mt-4 flex flex-col gap-2">
<Button v-if="passkey" variant="outline" :disabled="busy" @click="signInPasskey">Sign in with a passkey</Button>
<Button v-if="accounts.status?.oidc" variant="outline" as-child><a href="/oauth2/login">Sign in with OIDC</a></Button>
</div>
<Button v-if="!setup" variant="link" class="mt-3" :disabled="busy" @click="recovery = !recovery; error = ''">{{ recovery ? "Back to sign in" : "Use a recovery code" }}</Button>
</Card>
</main>
</template>