summaryrefslogtreecommitdiff
path: root/src/views/_ibs/ChangePassword/ChangePassword.vue
blob: b2ebc3b01d1f6cbac34c05b6df0ab88b2dd8633b (plain)
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
<template>
  <div class="change-password-container">
    <alert variant="danger" class="mb-4">
      <p v-if="changePasswordError">
        {{ $t('pageChangePassword.changePasswordError') }}
      </p>
      <p v-else>{{ $t('pageChangePassword.changePasswordAlertMessage') }}</p>
    </alert>
    <div class="change-password__form-container">
      <dl>
        <dt>{{ $t('pageChangePassword.username') }}</dt>
        <dd>{{ username }}</dd>
      </dl>
      <b-form novalidate @submit.prevent="changePassword">
        <b-form-group
          label-for="password"
          :label="$t('pageChangePassword.newPassword')"
        >
          <input-password-toggle>
            <b-form-input
              id="password"
              v-model="form.password"
              autofocus="autofocus"
              type="password"
              :state="getValidationState($v.form.password)"
              class="form-control-with-button"
              @change="$v.form.password.$touch()"
            >
            </b-form-input>
            <b-form-invalid-feedback role="alert">
              <template v-if="!$v.form.password.required">
                {{ $t('global.form.fieldRequired') }}
              </template>
            </b-form-invalid-feedback>
          </input-password-toggle>
        </b-form-group>
        <b-form-group
          label-for="password-confirm"
          :label="$t('pageChangePassword.confirmNewPassword')"
        >
          <input-password-toggle>
            <b-form-input
              id="password-confirm"
              v-model="form.passwordConfirm"
              type="password"
              :state="getValidationState($v.form.passwordConfirm)"
              class="form-control-with-button"
              @change="$v.form.passwordConfirm.$touch()"
            >
            </b-form-input>
            <b-form-invalid-feedback role="alert">
              <template v-if="!$v.form.passwordConfirm.required">
                {{ $t('global.form.fieldRequired') }}
              </template>
              <template v-else-if="!$v.form.passwordConfirm.sameAsPassword">
                {{ $t('global.form.passwordsDoNotMatch') }}
              </template>
            </b-form-invalid-feedback>
          </input-password-toggle>
        </b-form-group>
        <div class="text-right">
          <b-button type="button" variant="link" @click="goBack">
            {{ $t('pageChangePassword.goBack') }}
          </b-button>
          <b-button type="submit" variant="primary">
            {{ $t('pageChangePassword.changePassword') }}
          </b-button>
        </div>
      </b-form>
    </div>
  </div>
</template>

<script>
import { required, sameAs } from 'vuelidate/lib/validators';
import Alert from '@/components/_ibs/Global/Alert';
import VuelidateMixin from '@/components/_ibs/Mixins/VuelidateMixin';
import InputPasswordToggle from '@/components/_ibs/Global/InputPasswordToggle';
import BVToastMixin from '@/components/_ibs/Mixins/BVToastMixin';

export default {
  name: 'ChangePassword',
  components: { Alert, InputPasswordToggle },
  mixins: [VuelidateMixin, BVToastMixin],
  data() {
    return {
      form: {
        password: null,
        passwordConfirm: null,
      },
      username: this.$store.getters['global/username'],
      changePasswordError: false,
    };
  },
  validations() {
    return {
      form: {
        password: { required },
        passwordConfirm: {
          required,
          sameAsPassword: sameAs('password'),
        },
      },
    };
  },
  methods: {
    goBack() {
      // Remove session created if navigating back to the Login page
      this.$store.dispatch('authentication/logout');
    },
    changePassword() {
      this.$v.$touch();
      if (this.$v.$invalid) return;
      let data = {
        originalUsername: this.username,
        password: this.form.password,
      };

      this.$store
        .dispatch('userManagement/updateUser', data)
        .then(() => this.$router.push('/'))
        .catch(() => (this.changePasswordError = true));
    },
  },
};
</script>

<style lang="scss" scoped>
.change-password__form-container {
  @include media-breakpoint-up('md') {
    max-width: 360px;
  }
}
</style>