<script setup lang="ts">
const { restore } = useDngAuth()
const error = ref('')

onMounted(async () => {
  try {
    const session = await restore()
    if (!session) throw new Error('The sign-in link is invalid or expired.')
    const requested = localStorage.getItem('dng-post-auth-redirect')
    localStorage.removeItem('dng-post-auth-redirect')
    const destination = requested?.startsWith('/') && !requested.startsWith('//') ? requested : '/dashboard'
    await navigateTo(destination, { replace: true })
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : 'Could not complete sign-in.'
  }
})
</script>

<template>
  <div class="callback noise">
    <AppMark />
    <p v-if="!error">VERIFYING YOUR EMAIL…</p>
    <template v-else><p class="error">{{ error }}</p><NuxtLink to="/auth/sign-in">BACK TO SIGN IN</NuxtLink></template>
  </div>
</template>

<style scoped>
.callback{min-height:100vh;display:grid;place-content:center;justify-items:center;gap:28px}.callback p{font:500 10px var(--mono);letter-spacing:.16em;color:var(--muted)}.callback .error{max-width:420px;color:#ff9d85;text-align:center}.callback a{color:var(--acid);font:600 9px var(--mono)}
</style>
