|
| 1 | +<script setup lang="ts"> |
| 2 | +definePageMeta({ |
| 3 | + layout: "auth", |
| 4 | + middleware: ["guest"], |
| 5 | +}); |
| 6 | +
|
| 7 | +useSeoMeta({ |
| 8 | + title: "Reset Password — Reqcore", |
| 9 | + description: "Set a new password for your Reqcore account", |
| 10 | + robots: "noindex, nofollow", |
| 11 | +}); |
| 12 | +
|
| 13 | +const route = useRoute(); |
| 14 | +const newPassword = ref(""); |
| 15 | +const confirmPassword = ref(""); |
| 16 | +const error = ref(""); |
| 17 | +const success = ref(false); |
| 18 | +const isLoading = ref(false); |
| 19 | +const localePath = useLocalePath(); |
| 20 | +const { track } = useTrack(); |
| 21 | +
|
| 22 | +const token = computed(() => route.query.token as string | undefined); |
| 23 | +const tokenError = computed(() => route.query.error as string | undefined); |
| 24 | +
|
| 25 | +onMounted(() => track("reset_password_page_viewed")); |
| 26 | +
|
| 27 | +async function handleResetPassword() { |
| 28 | + error.value = ""; |
| 29 | +
|
| 30 | + if (!token.value) { |
| 31 | + error.value = "Invalid or missing reset token. Please request a new password reset link."; |
| 32 | + return; |
| 33 | + } |
| 34 | +
|
| 35 | + if (!newPassword.value) { |
| 36 | + error.value = "Password is required."; |
| 37 | + return; |
| 38 | + } |
| 39 | +
|
| 40 | + if (newPassword.value.length < 8) { |
| 41 | + error.value = "Password must be at least 8 characters."; |
| 42 | + return; |
| 43 | + } |
| 44 | +
|
| 45 | + if (newPassword.value !== confirmPassword.value) { |
| 46 | + error.value = "Passwords do not match."; |
| 47 | + return; |
| 48 | + } |
| 49 | +
|
| 50 | + isLoading.value = true; |
| 51 | +
|
| 52 | + try { |
| 53 | + const result = await authClient.resetPassword({ |
| 54 | + newPassword: newPassword.value, |
| 55 | + token: token.value, |
| 56 | + }); |
| 57 | +
|
| 58 | + if (result.error) { |
| 59 | + error.value = |
| 60 | + result.error.message ?? "Failed to reset password. The link may have expired."; |
| 61 | + isLoading.value = false; |
| 62 | + return; |
| 63 | + } |
| 64 | + } catch (e: unknown) { |
| 65 | + error.value = |
| 66 | + e instanceof Error ? e.message : "Failed to reset password. Please try again."; |
| 67 | + isLoading.value = false; |
| 68 | + return; |
| 69 | + } |
| 70 | +
|
| 71 | + track("reset_password_completed"); |
| 72 | + success.value = true; |
| 73 | + isLoading.value = false; |
| 74 | +} |
| 75 | +</script> |
| 76 | + |
| 77 | +<template> |
| 78 | + <div class="flex flex-col gap-4"> |
| 79 | + <h2 |
| 80 | + class="text-xl font-semibold text-center text-surface-900 dark:text-surface-100 mb-2" |
| 81 | + > |
| 82 | + Set new password |
| 83 | + </h2> |
| 84 | + |
| 85 | + <template v-if="success"> |
| 86 | + <div |
| 87 | + class="rounded-md border border-green-200 dark:border-green-800 bg-green-50 dark:bg-green-950 p-3 text-sm text-green-700 dark:text-green-400" |
| 88 | + > |
| 89 | + Your password has been reset successfully. |
| 90 | + </div> |
| 91 | + |
| 92 | + <NuxtLink |
| 93 | + :to="$localePath('/auth/sign-in')" |
| 94 | + class="mt-2 px-4 py-2.5 bg-brand-600 text-white rounded-md text-sm font-medium cursor-pointer hover:bg-brand-700 transition-colors text-center block" |
| 95 | + > |
| 96 | + Sign in with new password |
| 97 | + </NuxtLink> |
| 98 | + </template> |
| 99 | + |
| 100 | + <template v-else-if="tokenError || !token"> |
| 101 | + <div |
| 102 | + class="rounded-md border border-danger-200 dark:border-danger-800 bg-danger-50 dark:bg-danger-950 p-3 text-sm text-danger-700 dark:text-danger-400" |
| 103 | + > |
| 104 | + {{ tokenError === 'INVALID_TOKEN' |
| 105 | + ? "This password reset link is invalid or has expired." |
| 106 | + : "Invalid password reset link. Please request a new one." }} |
| 107 | + </div> |
| 108 | + |
| 109 | + <NuxtLink |
| 110 | + :to="$localePath('/auth/forgot-password')" |
| 111 | + class="mt-2 px-4 py-2.5 bg-brand-600 text-white rounded-md text-sm font-medium cursor-pointer hover:bg-brand-700 transition-colors text-center block" |
| 112 | + > |
| 113 | + Request new reset link |
| 114 | + </NuxtLink> |
| 115 | + </template> |
| 116 | + |
| 117 | + <template v-else> |
| 118 | + <div |
| 119 | + v-if="error" |
| 120 | + class="rounded-md border border-danger-200 dark:border-danger-800 bg-danger-50 dark:bg-danger-950 p-3 text-sm text-danger-700 dark:text-danger-400" |
| 121 | + > |
| 122 | + {{ error }} |
| 123 | + </div> |
| 124 | + |
| 125 | + <form class="flex flex-col gap-4" @submit.prevent="handleResetPassword"> |
| 126 | + <label |
| 127 | + class="flex flex-col gap-1 text-sm font-medium text-surface-700 dark:text-surface-300" |
| 128 | + > |
| 129 | + <span>New password</span> |
| 130 | + <input |
| 131 | + v-model="newPassword" |
| 132 | + type="password" |
| 133 | + autocomplete="new-password" |
| 134 | + required |
| 135 | + minlength="8" |
| 136 | + class="px-3 py-2 border border-surface-300 dark:border-surface-700 rounded-md text-sm text-surface-900 dark:text-surface-100 bg-white dark:bg-surface-800 outline-none transition-colors focus:border-brand-500 focus:ring-2 focus:ring-brand-500/15" |
| 137 | + /> |
| 138 | + </label> |
| 139 | + |
| 140 | + <label |
| 141 | + class="flex flex-col gap-1 text-sm font-medium text-surface-700 dark:text-surface-300" |
| 142 | + > |
| 143 | + <span>Confirm new password</span> |
| 144 | + <input |
| 145 | + v-model="confirmPassword" |
| 146 | + type="password" |
| 147 | + autocomplete="new-password" |
| 148 | + required |
| 149 | + minlength="8" |
| 150 | + class="px-3 py-2 border border-surface-300 dark:border-surface-700 rounded-md text-sm text-surface-900 dark:text-surface-100 bg-white dark:bg-surface-800 outline-none transition-colors focus:border-brand-500 focus:ring-2 focus:ring-brand-500/15" |
| 151 | + /> |
| 152 | + </label> |
| 153 | + |
| 154 | + <button |
| 155 | + type="submit" |
| 156 | + :disabled="isLoading" |
| 157 | + class="mt-2 px-4 py-2.5 bg-brand-600 text-white rounded-md text-sm font-medium cursor-pointer hover:bg-brand-700 disabled:opacity-60 disabled:cursor-not-allowed transition-colors" |
| 158 | + > |
| 159 | + {{ isLoading ? "Resetting…" : "Reset password" }} |
| 160 | + </button> |
| 161 | + </form> |
| 162 | + |
| 163 | + <p class="text-center text-sm text-surface-500 dark:text-surface-400"> |
| 164 | + <NuxtLink |
| 165 | + :to="$localePath('/auth/sign-in')" |
| 166 | + class="text-brand-600 dark:text-brand-400 hover:underline" |
| 167 | + > |
| 168 | + Back to sign in |
| 169 | + </NuxtLink> |
| 170 | + </p> |
| 171 | + </template> |
| 172 | + </div> |
| 173 | +</template> |
0 commit comments