All files / src/views ForgotPasswordPage.vue

94.66% Statements 71/75
80% Branches 52/65
83.33% Functions 10/12
95.89% Lines 70/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276  14x         1x 1x                     1x       12x                                     1x   39x       80x     1x 1x                     1x       5x                   1x   1x     5x                                 1x     1x   1x     5x                                                       1x                     41x                             14x 14x   14x 14x 14x 14x 14x 14x 14x 14x 14x 14x     12x   12x 12x 12x   12x 12x 11x 11x   1x 1x   12x         6x   5x 1x 1x     4x 4x   4x 4x             1x         3x 3x 1x 2x 1x 1x 1x         4x         3x 3x 3x   3x 3x 2x   1x 1x   3x                                                              
<template>
  <AuthLayout
    :presentation-title="t('auth.forgotPasswordTitle')"
    :presentation-subtitle="t('auth.forgotPasswordSubtitle')"
  >
    <div v-if="!codeSent" class="forgot-password-form">
      <h3 class="mb-4">{{ t('auth.resetPassword') }}</h3>
      <p class="text-muted mb-4">{{ t('auth.resetPasswordInstructions') }}</p>
 
      <form @submit.prevent="handleSendCode">
        <div v-if="error" class="alert alert-danger" role="alert">
          {{ error }}
        </div>
 
        <div v-if="success" class="alert alert-success" role="alert">
          {{ success }}
        </div>
 
        <div class="mb-3">
          <label for="email" class="form-label">{{ t('auth.email') }}</label>
          <input
            id="email"
            v-model="email"
            type="email"
            class="form-control"
            :placeholder="t('auth.emailPlaceholder')"
            required
            :disabled="loading"
          />
        </div>
 
        <button type="submit" class="btn btn-primary w-100 mb-3" :disabled="loading">
          <span
            v-if="loading"
            class="spinner-border spinner-border-sm me-2"
            role="status"
            aria-hidden="true"
          />
          {{ loading ? t('common.sending') : t('auth.sendResetCode') }}
        </button>
 
        <div class="text-center">
          <router-link to="/login" class="text-decoration-none">
            {{ t('auth.backToLogin') }}
          </router-link>
        </div>
      </form>
    </div>
 
    <div v-else class="reset-password-form">
      <h3 class="mb-4">{{ t('auth.enterNewPassword') }}</h3>
      <p class="text-muted mb-4">{{ t('auth.resetCodeSentTo').replace('{email}', email) }}</p>
 
      <form @submit.prevent="handleResetPassword">
        <div v-if="error" class="alert alert-danger" role="alert">
          {{ error }}
        </div>
 
        <div v-if="success" class="alert alert-success" role="alert">
          {{ success }}
        </div>
 
        <div class="mb-3">
          <label for="code" class="form-label">{{ t('auth.verificationCode') }}</label>
          <input
            id="code"
            v-model="code"
            type="text"
            class="form-control"
            :placeholder="t('auth.verificationCodePlaceholder')"
            required
            :disabled="loading"
            maxlength="6"
          />
        </div>
 
        <div class="mb-3">
          <label for="newPassword" class="form-label">{{ t('auth.newPassword') }}</label>
          <div class="input-group">
            <input
              id="newPassword"
              v-model="newPassword"
              :type="showNewPassword ? 'text' : 'password'"
              class="form-control"
              :placeholder="t('auth.newPasswordPlaceholder')"
              required
              :disabled="loading"
              minlength="8"
            />
            <button
              type="button"
              class="btn btn-outline-secondary"
              :aria-label="showNewPassword ? t('auth.hidePassword') : t('auth.showPassword')"
              @click="showNewPassword = !showNewPassword"
            >
              <FontAwesomeIcon :icon="showNewPassword ? 'eye-slash' : 'eye'" />
            </button>
          </div>
          <small class="text-muted">{{ t('auth.passwordRequirements') }}</small>
        </div>
 
        <div class="mb-3">
          <label for="confirmPassword" class="form-label">{{ t('auth.confirmNewPassword') }}</label>
          <div class="input-group">
            <input
              id="confirmPassword"
              v-model="confirmPassword"
              :type="showConfirmPassword ? 'text' : 'password'"
              class="form-control"
              :placeholder="t('auth.confirmNewPasswordPlaceholder')"
              required
              :disabled="loading"
            />
            <button
              type="button"
              class="btn btn-outline-secondary"
              :aria-label="showConfirmPassword ? t('auth.hidePassword') : t('auth.showPassword')"
              @click="showConfirmPassword = !showConfirmPassword"
            >
              <FontAwesomeIcon :icon="showConfirmPassword ? 'eye-slash' : 'eye'" />
            </button>
          </div>
        </div>
 
        <button type="submit" class="btn btn-primary w-100 mb-3" :disabled="loading">
          <span
            v-if="loading"
            class="spinner-border spinner-border-sm me-2"
            role="status"
            aria-hidden="true"
          />
          {{ loading ? t('common.saving') : t('auth.resetPassword') }}
        </button>
 
        <div class="text-center">
          <button
            type="button"
            class="btn btn-link text-decoration-none p-0"
            :disabled="loading"
            @click="handleResendCode"
          >
            {{ t('auth.resendCode') }}
          </button>
          <span class="mx-2">•</span>
          <router-link to="/login" class="text-decoration-none">
            {{ t('auth.backToLogin') }}
          </router-link>
        </div>
      </form>
    </div>
  </AuthLayout>
</template>
 
<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { resetPassword, confirmResetPassword } from 'aws-amplify/auth'
import AuthLayout from '@/components/AuthLayout.vue'
import { useUILanguage } from '@/composables/useUILanguage'
 
const router = useRouter()
const { t } = useUILanguage()
 
const email = ref('')
const code = ref('')
const newPassword = ref('')
const confirmPassword = ref('')
const showNewPassword = ref(false)
const showConfirmPassword = ref(false)
const loading = ref(false)
const error = ref('')
const success = ref('')
const codeSent = ref(false)
 
async function handleSendCode() {
  Iif (!email.value) return
 
  loading.value = true
  error.value = ''
  success.value = ''
 
  try {
    await resetPassword({ username: email.value })
    codeSent.value = true
    success.value = t('auth.resetCodeSent')
  } catch (err: any) {
    console.error('Error sending reset code:', err)
    error.value = err.message || t('auth.resetCodeError')
  } finally {
    loading.value = false
  }
}
 
async function handleResetPassword() {
  if (!code.value || !newPassword.value || !confirmPassword.value) return
 
  if (newPassword.value !== confirmPassword.value) {
    error.value = t('auth.passwordsDoNotMatch')
    return
  }
 
  loading.value = true
  error.value = ''
 
  try {
    await confirmResetPassword({
      username: email.value,
      confirmationCode: code.value,
      newPassword: newPassword.value
    })
 
    // Success - redirect to login with success message
    router.push({
      path: '/login',
      query: { resetSuccess: 'true' }
    })
  } catch (err: any) {
    console.error('Error resetting password:', err)
    if (err.name === 'CodeMismatchException') {
      error.value = t('auth.invalidVerificationCode')
    } else if (err.name === 'ExpiredCodeException') {
      error.value = t('auth.expiredVerificationCode')
    } else if (err.name === 'InvalidPasswordException') {
      error.value = t('auth.passwordRequirementsNotMet')
    } else E{
      error.value = err.message || t('auth.resetPasswordError')
    }
  } finally {
    loading.value = false
  }
}
 
async function handleResendCode() {
  loading.value = true
  error.value = ''
  success.value = ''
 
  try {
    await resetPassword({ username: email.value })
    success.value = t('auth.resetCodeResent')
  } catch (err: any) {
    console.error('Error resending code:', err)
    error.value = err.message || t('auth.resetCodeError')
  } finally {
    loading.value = false
  }
}
</script>
 
<style scoped>
.forgot-password-form,
.reset-password-form {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}
 
h3 {
  text-align: center;
  color: #333;
}
 
.form-label {
  font-weight: 500;
}
 
.btn-link {
  color: #0d6efd;
}
 
.btn-link:hover {
  color: #0a58ca;
  text-decoration: underline !important;
}
</style>