Ignore:
Timestamp:
06/26/26 16:32:12 (45 hours ago)
Author:
veronika-ils <ilioskaveronika@…>
Branches:
master
Parents:
fa32d0f
Message:

add functionality so that users can change passwords

File:
1 edited

Legend:

Unmodified
Added
Removed
  • petify-frontend/src/views/ProfileView.vue

    rfa32d0f rae83647  
    9494              </button>
    9595            </li>
     96            <li class="nav-item" role="presentation">
     97              <button
     98                class="nav-link"
     99                :class="{ active: activeTab === 'account' }"
     100                @click="activeTab = 'account'"
     101                type="button"
     102                role="tab"
     103              >
     104                <i class="bi bi-shield-lock-fill"></i> Account
     105              </button>
     106            </li>
    96107          </ul>
     108
     109          <!-- Account Tab -->
     110          <div v-if="activeTab === 'account'" class="tab-content-section">
     111            <h2 class="section-title">Account</h2>
     112            <form class="password-form" @submit.prevent="submitPasswordChange">
     113              <div v-if="passwordSuccess" class="alert alert-success" role="alert">
     114                {{ passwordSuccess }}
     115              </div>
     116              <div v-if="passwordError" class="alert alert-danger" role="alert">
     117                {{ passwordError }}
     118              </div>
     119
     120              <div class="mb-3">
     121                <label class="form-label" for="current-password">Current password</label>
     122                <input
     123                  id="current-password"
     124                  v-model="passwordForm.currentPassword"
     125                  class="form-control"
     126                  type="password"
     127                  autocomplete="current-password"
     128                  required
     129                />
     130              </div>
     131
     132              <div class="mb-3">
     133                <label class="form-label" for="new-password">New password</label>
     134                <input
     135                  id="new-password"
     136                  v-model="passwordForm.newPassword"
     137                  class="form-control"
     138                  type="password"
     139                  autocomplete="new-password"
     140                  minlength="8"
     141                  required
     142                />
     143              </div>
     144
     145              <div class="mb-4">
     146                <label class="form-label" for="confirm-new-password">Confirm new password</label>
     147                <input
     148                  id="confirm-new-password"
     149                  v-model="passwordForm.confirmPassword"
     150                  class="form-control"
     151                  type="password"
     152                  autocomplete="new-password"
     153                  minlength="8"
     154                  required
     155                />
     156              </div>
     157
     158              <button class="btn btn-primary" type="submit" :disabled="isPasswordSubmitting">
     159                <span v-if="isPasswordSubmitting" class="spinner-border spinner-border-sm me-2" aria-hidden="true"></span>
     160                {{ isPasswordSubmitting ? 'Changing...' : 'Change password' }}
     161              </button>
     162            </form>
     163          </div>
    97164
    98165          <!-- Listings Tab -->
     
    746813  type Review,
    747814} from '../api/reviews'
     815import { changePassword } from '../api/auth'
    748816
    749817const router = useRouter()
    750818const auth = useAuthStore()
    751819
    752 const activeTab = ref<'listings' | 'pets' | 'create-listing' | 'favorites' | 'appointments'>('listings')
     820const activeTab = ref<'listings' | 'pets' | 'create-listing' | 'favorites' | 'appointments' | 'account'>('listings')
    753821const listings = ref<any[]>([])
    754822const pets = ref<any[]>([])
     
    782850const petPhotoFile = ref<File | null>(null)
    783851const petPhotoPreview = ref('')
     852const isPasswordSubmitting = ref(false)
     853const passwordError = ref('')
     854const passwordSuccess = ref('')
     855
     856const passwordForm = ref({
     857  currentPassword: '',
     858  newPassword: '',
     859  confirmPassword: '',
     860})
    784861
    785862const newListing = ref({
     
    859936  // Fall back to petNameMap (from pets list)
    860937  return petNameMap.value[animalId] || 'Unknown Pet'
     938}
     939
     940async function submitPasswordChange() {
     941  passwordError.value = ''
     942  passwordSuccess.value = ''
     943
     944  if (!auth.user?.userId) {
     945    passwordError.value = 'You need to be logged in to change your password.'
     946    return
     947  }
     948  if (passwordForm.value.newPassword.length < 8) {
     949    passwordError.value = 'New password must be at least 8 characters long.'
     950    return
     951  }
     952  if (passwordForm.value.newPassword !== passwordForm.value.confirmPassword) {
     953    passwordError.value = 'New passwords do not match.'
     954    return
     955  }
     956
     957  isPasswordSubmitting.value = true
     958  try {
     959    await changePassword({
     960      userId: auth.user.userId,
     961      currentPassword: passwordForm.value.currentPassword,
     962      newPassword: passwordForm.value.newPassword,
     963    })
     964    passwordForm.value = {
     965      currentPassword: '',
     966      newPassword: '',
     967      confirmPassword: '',
     968    }
     969    passwordSuccess.value = 'Password changed successfully.'
     970  } catch (error: any) {
     971    passwordError.value = error?.message || 'Failed to change password.'
     972  } finally {
     973    isPasswordSubmitting.value = false
     974  }
    861975}
    862976
     
    16701784}
    16711785
     1786.password-form {
     1787  max-width: 520px;
     1788}
     1789
    16721790@keyframes fadeIn {
    16731791  from {
Note: See TracChangeset for help on using the changeset viewer.