logigate/app/components/editor/AccountDialog.vue

140 lines
6.2 KiB
Vue
Raw Normal View History

<script setup lang="ts">
const ed = useEditor()
const close = () => (ed.dialog.value = null)
const mode = ref<'signin' | 'create'>('signin')
const email = ref('')
const password = ref('')
const error = ref('')
const busy = ref(false)
const current = ref('')
const next = ref('')
const removeHere = ref(false)
const deletePw = ref('')
const panel = ref<'' | 'password' | 'delete'>('')
const STATE: Record<string, string> = {
syncing: 'Syncing…',
synced: 'All projects are saved in your account',
offline: 'Offline. Changes are kept here and sync when you are back online.',
error: 'Sync failed',
}
async function run(fn: () => Promise<unknown>) {
error.value = ''
busy.value = true
try {
await fn()
return true
} catch (e) {
error.value = (e as Error).message
return false
} finally {
busy.value = false
}
}
async function submit() {
if (await run(() => ed.signIn(email.value, password.value, mode.value === 'create'))) {
password.value = ''
close()
}
}
async function savePassword() {
if (await run(() => ed.changePassword(current.value, next.value))) {
current.value = next.value = ''
panel.value = ''
}
}
async function signOut() {
if (await run(() => ed.signOut(removeHere.value))) close()
}
async function deleteAccount() {
if (await run(() => ed.deleteAccount(deletePw.value))) close()
}
</script>
<template>
<div class="modal-backdrop" @click.self="close">
<div class="modal account" role="dialog" aria-labelledby="account-title">
<div class="flex items-start justify-between gap-4">
<h2 id="account-title">{{ ed.account.value ? 'Your account' : mode === 'signin' ? 'Sign in' : 'Create an account' }}</h2>
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
</div>
<template v-if="!ed.account.value">
<p class="hint mt-1">
With an account your projects follow you to every computer and browser you sign in on. Projects you already have here are added to it.
</p>
<div class="seg mb-4">
<button :class="{ active: mode === 'signin' }" @click="mode = 'signin'; error = ''">Sign in</button>
<button :class="{ active: mode === 'create' }" @click="mode = 'create'; error = ''">Create account</button>
</div>
<form class="grid gap-1" @submit.prevent="submit">
<label class="field">
<span>Email</span>
<input v-model="email" type="email" autocomplete="email" required />
</label>
<label class="field">
<span>Password{{ mode === 'create' ? ' (at least 8 characters)' : '' }}</span>
<input v-model="password" type="password" :autocomplete="mode === 'create' ? 'new-password' : 'current-password'" required minlength="8" />
</label>
<p v-if="error" class="result fail"><Icon name="lucide:circle-x" class="size-4 shrink-0" /> {{ error }}</p>
<button class="btn btn-primary mt-2" type="submit" :disabled="busy">
<Icon :name="busy ? 'lucide:loader' : mode === 'signin' ? 'lucide:log-in' : 'lucide:user-plus'" class="size-4" :class="{ 'animate-spin': busy }" />
{{ mode === 'signin' ? 'Sign in' : 'Create account' }}
</button>
</form>
<p class="hint mt-4">
Stored on Blax Software's server: your email, your password in scrambled form (scrypt), and your projects. Nothing is shared or used for anything else, and you can delete the account any time.
</p>
</template>
<template v-else>
<p class="mt-2 text-sm">Signed in as <strong>{{ ed.account.value.email }}</strong></p>
<p class="result" :class="ed.cloudState.value === 'error' ? 'fail' : 'pass'">
<Icon
:name="ed.cloudState.value === 'synced' ? 'lucide:cloud-check' : ed.cloudState.value === 'syncing' ? 'lucide:refresh-cw' : 'lucide:cloud-off'"
class="size-4 shrink-0"
:class="{ 'animate-spin': ed.cloudState.value === 'syncing' }"
/>
{{ STATE[ed.cloudState.value] }}<template v-if="ed.cloudState.value === 'error'">: {{ ed.cloudError.value }}</template>
</p>
<div class="mt-3 flex flex-wrap gap-2">
<button class="btn" @click="ed.syncAccount()"><Icon name="lucide:refresh-cw" class="size-4" /> Sync now</button>
<button class="btn" @click="panel = panel === 'password' ? '' : 'password'"><Icon name="lucide:key-round" class="size-4" /> Change password</button>
</div>
<form v-if="panel === 'password'" class="mt-3 grid gap-1" @submit.prevent="savePassword">
<label class="field"><span>Current password</span><input v-model="current" type="password" autocomplete="current-password" required /></label>
<label class="field"><span>New password (at least 8 characters)</span><input v-model="next" type="password" autocomplete="new-password" required minlength="8" /></label>
<button class="btn btn-primary" type="submit" :disabled="busy">Save new password</button>
</form>
<h3>Sign out</h3>
<label class="check">
<input v-model="removeHere" type="checkbox" />
Also remove my projects from this browser (they stay in the account)
</label>
<button class="btn" :disabled="busy" @click="signOut"><Icon name="lucide:log-out" class="size-4" /> Sign out</button>
<h3>Delete account</h3>
<template v-if="panel !== 'delete'">
<button class="btn btn-ghost" @click="panel = 'delete'"><Icon name="lucide:trash-2" class="size-4" /> Delete account…</button>
</template>
<form v-else class="grid gap-1" @submit.prevent="deleteAccount">
<p class="hint">Deletes the account and every project stored in it. The projects in this browser stay here.</p>
<label class="field"><span>Password</span><input v-model="deletePw" type="password" autocomplete="current-password" required /></label>
<button class="btn btn-danger" type="submit" :disabled="busy">Delete account for good</button>
</form>
<p v-if="error" class="result fail"><Icon name="lucide:circle-x" class="size-4 shrink-0" /> {{ error }}</p>
</template>
</div>
</div>
</template>