Files
archie/ui/src/accounts/AccountPage.vue
T

65 lines
5.6 KiB
Vue

<script setup lang="ts">
import { Item, ItemActions, ItemContent, ItemGroup } from "@/components/ui/item";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { computed, onMounted, ref } from "vue";
import PageHeader from "@/base/PageHeader.vue";
import LinkedChannels from "@/accounts/LinkedChannels.vue";
import PersonalTokensCard from "@/accounts/PersonalTokensCard.vue";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { SettingRow } from "@/components/ui/setting-row";
import { Section } from "@/components/ui/section";
import { api, classifyActionError, describeError } from "@/lib/api";
import { errors } from "@/copy/errors";
import { passkeysAvailable,useAccountsStore,usePasskey } from "./accounts";
const recovery=ref("");
const makeRecovery=()=>run(async()=>{recovery.value=(await api.recoveryCode()).code});
const accounts=useAccountsStore();const keys=ref<{id:string;name:string}[]>([]);const pending=ref<{id:string;name:string}|null>(null);const error=ref("");const sessionExpired=ref(false);const busy=ref(false);const keyName=ref("");const current=ref("");const next=ref("");
const eligible=computed(()=>passkeysAvailable(accounts.status?.public_url??""));
async function load(){try{keys.value=(await api.passkeys()).passkeys}catch(cause){error.value=describeError(cause, errors.common.unknown);sessionExpired.value=classifyActionError(cause).kind==="session-expired"}}
onMounted(load);
async function run(action:()=>Promise<unknown>){busy.value=true;error.value="";try{await action()}catch(cause){error.value=describeError(cause, errors.common.unknown);sessionExpired.value=classifyActionError(cause).kind==="session-expired"}finally{busy.value=false}}
const enroll=()=>run(async()=>{await usePasskey(true,keyName.value);keyName.value="";await load()});
const remove=(id:string)=>run(async()=>{await api.deletePasskey(id);await load()});
const change=()=>run(async()=>{await api.changePassword(current.value,next.value);current.value=next.value="";window.location.assign("/")});
</script>
<template>
<div class="mx-auto max-w-[80ch]">
<PageHeader title="Your account"><span class="text-label text-fg-muted">{{accounts.status?.user?.display_name}}</span></PageHeader>
<Alert v-if="$route.query.welcome" class="mb-4">Your organisation and admin account are ready. Add a passkey below, <RouterLink class="underline" to="/org/members">add members</RouterLink>, or <RouterLink class="underline" to="/settings/authentication">configure OIDC</RouterLink>.</Alert>
<Alert v-if="error" variant="destructive" class="mb-4"><AlertDescription>{{error}}<a v-if="sessionExpired" class="underline" href="/?reauth=1"> Sign in again</a></AlertDescription></Alert>
<LinkedChannels :welcome="Boolean($route.query.welcome)" />
<Section title="Passkeys">
<ItemGroup v-if="keys.length" class="mb-3"><Item v-for="key in keys" :key="key.id" as="li" variant="outline" size="xs"><ItemContent class="min-w-0 truncate"><span class="truncate">{{key.name}}</span></ItemContent><ItemActions><Button variant="ghost" size="sm" class="text-danger" :disabled="busy" @click="pending = key">Remove</Button></ItemActions></Item></ItemGroup>
<form v-if="eligible" class="flex flex-wrap gap-2" @submit.prevent="enroll"><Input v-model="keyName" aria-label="Passkey name" placeholder="Passkey name (optional)" maxlength="200" class="min-w-40 flex-1"/><Button type="submit" :disabled="busy">Add passkey</Button></form>
<p v-else class="text-fg-muted">Passkeys need an HTTPS domain or localhost. Set the instance URL in <RouterLink class="underline" to="/settings/authentication">Authentication settings</RouterLink>.</p>
</Section>
<PersonalTokensCard />
<Section title="Recovery code" description="Shown once. Use it to reset your password if you lose access. A new code replaces the old one.">
<template #action><Button size="sm" :disabled="busy" @click="makeRecovery">Generate</Button></template>
<Card v-if="recovery" size="sm"><p class="break-all font-mono" role="status">{{recovery}}</p></Card>
</Section>
<Section title="Change password" description="Changing your password signs out all your sessions.">
<form @submit.prevent="change">
<SettingRow label="Current password" for="current-password"><Input id="current-password" v-model="current" type="password" autocomplete="current-password" required class="max-w-sm" /></SettingRow>
<SettingRow label="New password" for="new-password"><Input id="new-password" v-model="next" type="password" autocomplete="new-password" required minlength="15" maxlength="1024" class="max-w-sm" /></SettingRow>
<div class="flex justify-end border-t border-border pt-3"><Button type="submit" :disabled="busy">Change password</Button></div>
</form>
</Section>
</div>
<AlertDialog :open="pending !== null" @update:open="(v: boolean) => { if (!v) pending = null; }">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Remove passkey?</AlertDialogTitle>
<AlertDialogDescription>Remove the passkey "{{ pending?.name }}"? You will no longer be able to sign in with it.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction @click="if (pending) { const k = pending; pending = null; remove(k.id); }">Remove passkey</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>