2026-09-03 20:04:49 +01:00
|
|
|
<script setup lang="ts">
|
2026-09-04 14:47:46 +01:00
|
|
|
import { onBeforeUnmount, ref } from 'vue'
|
2026-09-03 20:04:49 +01:00
|
|
|
import { ApiError } from '../lib/api'
|
|
|
|
|
import { useAuthStore } from '../stores/auth'
|
|
|
|
|
|
|
|
|
|
const auth = useAuthStore()
|
|
|
|
|
|
|
|
|
|
const cooldown = ref(0)
|
|
|
|
|
let timer: ReturnType<typeof setInterval> | undefined
|
|
|
|
|
|
|
|
|
|
function startCooldown(seconds: number) {
|
|
|
|
|
cooldown.value = Math.max(0, Math.ceil(seconds))
|
|
|
|
|
clearInterval(timer)
|
|
|
|
|
timer = setInterval(() => {
|
|
|
|
|
cooldown.value -= 1
|
|
|
|
|
if (cooldown.value <= 0) clearInterval(timer)
|
|
|
|
|
}, 1000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onBeforeUnmount(() => clearInterval(timer))
|
|
|
|
|
|
|
|
|
|
// --- change email ------------------------------------------------------
|
|
|
|
|
const newEmail = ref('')
|
|
|
|
|
const changing = ref(false)
|
|
|
|
|
const changeMessage = ref('')
|
|
|
|
|
const changeError = ref<ApiError | null>(null)
|
|
|
|
|
|
|
|
|
|
async function onChangeEmail() {
|
|
|
|
|
changing.value = true
|
|
|
|
|
changeMessage.value = ''
|
|
|
|
|
changeError.value = null
|
|
|
|
|
try {
|
2026-09-04 14:47:46 +01:00
|
|
|
const { pending_email, retry_after } = await auth.requestEmailChange(newEmail.value)
|
2026-09-03 20:04:49 +01:00
|
|
|
changeMessage.value = `Confirmation link sent to ${pending_email}. Your address changes once you open it.`
|
|
|
|
|
newEmail.value = ''
|
|
|
|
|
startCooldown(retry_after)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
changeError.value = e instanceof ApiError ? e : new ApiError('Could not request the change.', 0)
|
|
|
|
|
if (e instanceof ApiError && e.status === 429) startCooldown(e.retryAfter ?? 60)
|
|
|
|
|
} finally {
|
|
|
|
|
changing.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<section class="card">
|
2026-09-04 14:47:46 +01:00
|
|
|
<p><RouterLink to="/">← Dashboard</RouterLink></p>
|
2026-09-03 20:04:49 +01:00
|
|
|
<h1>Your profile</h1>
|
|
|
|
|
|
|
|
|
|
<p><strong>Email:</strong> {{ auth.user?.email }}</p>
|
|
|
|
|
|
|
|
|
|
<div v-if="auth.user?.pending_email" class="notice">
|
|
|
|
|
A change to <strong>{{ auth.user.pending_email }}</strong> is pending. Open the
|
|
|
|
|
link we sent to that address to complete it. The link expires 15 minutes
|
|
|
|
|
after it was sent.
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
|
<h2>Change email address</h2>
|
2026-09-04 14:47:46 +01:00
|
|
|
<p class="muted">
|
|
|
|
|
We'll email a confirmation link to the new address; the change only
|
|
|
|
|
takes effect once you open it.
|
|
|
|
|
</p>
|
2026-09-03 20:04:49 +01:00
|
|
|
<form class="form" @submit.prevent="onChangeEmail">
|
|
|
|
|
<label>
|
|
|
|
|
<span>New email</span>
|
|
|
|
|
<input v-model="newEmail" type="email" maxlength="255" required />
|
|
|
|
|
<small v-if="changeError?.fieldError('email')" class="field-error">
|
|
|
|
|
{{ changeError.fieldError('email') }}
|
|
|
|
|
</small>
|
|
|
|
|
</label>
|
|
|
|
|
|
|
|
|
|
<p v-if="changeError && Object.keys(changeError.details).length === 0" class="form-error">
|
|
|
|
|
{{ changeError.message }}
|
|
|
|
|
</p>
|
|
|
|
|
<p v-if="changeMessage" class="muted">{{ changeMessage }}</p>
|
|
|
|
|
|
|
|
|
|
<button type="submit" :disabled="changing || cooldown > 0">
|
|
|
|
|
{{ changing ? 'Sending…' : cooldown > 0 ? `Wait ${cooldown}s` : 'Send confirmation link' }}
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
</section>
|
|
|
|
|
</section>
|
|
|
|
|
</template>
|