Files
project-manager/web/src/views/ProfileView.vue
T

86 lines
2.8 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue'
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 {
const { pending_email, retry_after } = await auth.requestEmailChange(newEmail.value)
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">
<p><RouterLink to="/">&larr; Dashboard</RouterLink></p>
<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>
<p class="muted">
We'll email a confirmation link to the new address; the change only
takes effect once you open it.
</p>
<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>