summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/AccountModule.vue
blob: a39a414a6e1b2f1eeaf7acd33c43cd0aa4f72797 (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<i18n>
{
  "en": {
    "account": "User Account",
    "info": "Info",
    "deleteAccount": "Delete Account",
    "deleteAccountMsg": "Are you sure you want to permnanently delete your account?",
    "deleteAccountMsg2": "This action cannot be undone",
    "email": "E-Mail",
    "emailError": "E-mail must be valid.",
    "name": "Name",
    "password": "Password",
    "passwordConfirm": "Retype password",
    "passwordChanged": "Password successfully changed.",
    "passwordCurrent": "Current password",
    "passwordEmptyError": "Password can not be empty.",
    "passwordInvalidError": "Password incorrect.",
    "passwordLengthError": "Minimum 8 characters required.",
    "passwordMatchError": "Passwords do not match.",
    "passwordNew": "New password",
    "passwordNotDifferentError": "New password must be different than the current password.",
    "passwordRequirement1": ">= 8 characters",
    "passwordRequirement2": "0+ [0-9]",
    "passwordRequirement3": "0+ [a-z]",
    "passwordRequirement4": "0+ [A-Z]",
    "passwordRequirement5": "0+ [!\"§$%&/()=?+#*.:,;]",
    "passwordRequirements": "Password requirements",
    "security": "Security",
    "username": "Username",
    "userInfoChanged": "User informations successfully changed."
  },
  "de": {
    "account": "Benutzerkonto",
    "info": "Info",
    "deleteAccount": "Account Löschen",
    "deleteAccountMsg": "Sind sie sicher, dass sie ihren Account entgültig Löschen wollen?",
    "deleteAccountMsg2": "Diese Aktion kann nicht rückgängig gemacht werden.",
    "email": "E-Mail",
    "emailError": "Keine gültige E-Mail.",
    "name": "Name",
    "password": "Passwort",
    "passwordChanged": "Passwort wurde erfolgreich geändert.",
    "passwordConfirm": "Passwort wiederholen",
    "passwordCurrent": "Aktuelles Passwort",
    "passwordEmptyError": "Passwort kann nicht leer sein.",
    "passwordInvalidError": "Falsches Passwort.",
    "passwordLengthError": "Es sind mindestens 8 Zeichen erforderlich.",
    "passwordMatchError": "Passwörter stimmen nicht überein.",
    "passwordNew": "Neues Password",
    "passwordNotDifferentError": "Das neue Passwort muss sich vom alten unterscheiden.",
    "passwordRequirement1": ">= 8 Zeichen",
    "passwordRequirement2": "0+ [0-9]",
    "passwordRequirement3": "0+ [a-z]",
    "passwordRequirement4": "0+ [A-Z]",
    "passwordRequirement5": "0+ [!\"§$%&/()=?+#*'-_.:,;]",
    "passwordRequirements": "Passwort anforderungen",
    "security": "Sicherheit",
    "username": "Benutzername",
    "userInfoChanged": "Benutzer Informationen wurden erfolgreich geändert"
  }
}
</i18n>

<template>
  <v-container fill-height class="">
    <v-layout>
      <v-flex xl10 offset-xl1 lg12>
      <v-card class="tabbar-card">
        <v-tabs :dark="tabsDark" :color="tabsColor" :slider-color="tabsSliderColor"
          v-model="tab"
          grow
          hide-slider
        >
          <v-tab>
            <v-icon class="tabbar-tabicon">assignment_ind</v-icon>
            {{ $t('account') }}
          </v-tab>
        </v-tabs>
      </v-card>
      <v-tabs-items v-model="tab">
        <v-tab-item>

          <v-subheader>{{ $t('info') }}</v-subheader>
          <v-card>
            <v-card-text>
              <v-layout wrap>

                <v-flex lg3 md6 sm6 xs12 order-lg2 order-xs2>
                  <v-layout column>
                    <div class="info-input">
                      <div class="body-2 info-heading"><v-icon>account_circle</v-icon><span>{{ $t('username') }}</span></div>
                      <div class="info-text">{{ user.username }}</div>
                    </div>
                  </v-layout>
                </v-flex>

                <v-flex lg3 md6 sm6 xs12 order-lg4 order-xs4>
                  <v-layout column>
                    <div class="info-input">
                      <div class="body-2 info-heading"><v-icon>label</v-icon><span>{{ $t('name') }}</span></div>
                      <div v-if="!editInfoMode" class="info-text">{{ user.name }}</div>
                      <v-text-field v-else v-model="infoName" class="info-text pa-0" hide-details></v-text-field>
                    </div>
                  </v-layout>
                </v-flex>

                <v-flex lg3 md6 sm6 xs12 order-lg6 order-xs6>
                  <v-layout column>
                    <div class="info-input">
                      <div class="body-2 info-heading"><v-icon>email</v-icon><span>{{ $t('email') }}</span></div>
                      <div v-if="!editInfoMode" class="info-text">{{ user.email }}</div>
                      <v-text-field
                        v-else
                        v-model="infoEmail"
                        :rules="emailRules"
                        ref="emailField"
                        validate-on-blur
                        class="info-text pa-0"
                        hide-details
                      ></v-text-field>
                    </div>
                  </v-layout>
                </v-flex>

                <v-flex lg3 md6 sm6 xs12 order-lg7 order-md3 order-sm3 order-xs1 class="text-xs-right">
                  <div class="info-input">
                    <div v-if="!editInfoMode">
                      <v-btn color="primary" flat @click="editInfo" class="info-buttons tutorial-element label-left" style="--label-number: '1'">
                        <v-icon left>create</v-icon>{{ $t('edit') }}
                      </v-btn>
                    </div>
                    <div v-else>
                      <v-btn color="primary" flat @click="cancelEditInfo" class="info-buttons">{{ $t('cancel') }}</v-btn>
                      <v-btn color="primary" @click="submitInfo" class="info-buttons">
                        <v-icon left>save</v-icon>{{ $t('save') }}
                      </v-btn>
                    </div>
                  </div>
                </v-flex>

              </v-layout>
            </v-card-text>
          </v-card>

          <v-subheader>{{ $t('security') }}</v-subheader>
          <v-card>
            <v-card-text>
              <v-layout wrap>

                <v-flex lg4 md6 sm6 xs12 order-lg1 order-xs2>
                  <v-layout column>
                    <div class="info-input">
                      <div class="body-2 info-heading"><v-icon>lock</v-icon><span>{{ $t('password') }}</span></div>
                      <div v-if="!editPasswordMode" class="info-text">********</div>
                      <div v-else class="info-text">
                        <v-form ref="passwordForm" v-model="valid" lazy-validation>
                          <v-text-field
                            type="password"
                            v-model="passwordCurrent"
                            :rules="currentPasswordRules"
                            @input="clearCurrentPasswordErrors"
                          >
                            <template slot="label">{{ $t('passwordCurrent') }} <label class="error--text">*</label></template>
                          </v-text-field>
                           <v-text-field
                            validate-on-blur
                            type="password"
                            v-model="passwordNew"
                            :rules="passwordRules"
                          >
                            <template slot="label">{{ $t('passwordNew') }} <label class="error--text">*</label></template>
                           </v-text-field>
                          <v-text-field
                            validate-on-blur
                            type="password"
                            v-model="passwordConfirm"
                            :rules="confirmPasswordRules"
                          >
                            <template slot="label">{{ $t('passwordConfirm') }} <label class="error--text">*</label></template>
                          </v-text-field>
                        </v-form>
                      </div>
                    </div>
                  </v-layout>
                </v-flex>

                <v-flex lg4 md6 sm6 xs12 order-lg1 order-xs3 v-if="editPasswordMode">
                  <v-layout column>
                    <div class="info-input">
                      <div class="body-2 info-heading"><v-icon>ballot</v-icon><span>{{ $t('passwordRequirements') }}</span></div>
                      <div class="info-text">{{ $t('passwordRequirement1') }}</div>
                      <div class="info-text">{{ $t('passwordRequirement2') }}</div>
                      <div class="info-text">{{ $t('passwordRequirement3') }}</div>
                      <div class="info-text">{{ $t('passwordRequirement4') }}</div>
                      <div class="info-text">{{ $t('passwordRequirement5') }}</div>
                    </div>
                  </v-layout>
                </v-flex>

                <v-flex v-if="!editPasswordMode" lg8 sm6 xs12 order-lg3 order-sm2 order-xs1 class="text-xs-right">
                  <div class="info-input">
                    <v-btn color="primary" flat @click="editPassword" class="info-buttons tutorial-element label-left" style="--label-number: '2'">
                      <v-icon left>create</v-icon>{{ $t('edit') }}
                    </v-btn>
                  </div>
                </v-flex>

                <v-flex v-else lg4 sm12 xs12 order-lg3 order-sm1 order-xs1 class="text-xs-right">
                  <div class="info-input">
                      <v-btn color="primary" flat @click="cancelEditPassword" class="info-buttons">{{ $t('cancel') }}</v-btn>
                      <v-btn color="primary" @click="submitPassword" class="info-buttons">
                        <v-icon left>save</v-icon>{{ $t('save') }}
                      </v-btn>
                  </div>
                </v-flex>

              </v-layout>
            </v-card-text>
          </v-card>
          <v-spacer></v-spacer>
          <div style="display: flex; justify-content: center;">
            <v-btn
              class="delete-button tutorial-element label-left" style="--label-number: '3'"
              color="error"
              @click="dialog = true"
            ><v-icon left>delete</v-icon>{{ $t('deleteAccount') }}</v-btn>
          </div>
        </v-tab-item>
      </v-tabs-items>
      </v-flex>
    </v-layout>

    <v-dialog
      :value="dialog"
      scrollable
      max-width="500px"
      :fullscreen="$vuetify.breakpoint.smAndDown"
    >
      <v-card>
        <v-card-title class="elevation-3">
          <div class="headline">{{ $t('deleteAccount') }}</div>
        </v-card-title>
        <v-card-text>
          <div>{{ $t('deleteAccountMsg') }}</div>
          <span class="grey--text">{{ $t('deleteAccountMsg2') }}</span>
        </v-card-text>
        <v-divider></v-divider>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn flat="flat" @click="dialog = false">{{ $t('cancel') }}</v-btn>
          <v-btn color="error" @click="deleteAccount">{{ $t('delete') }}</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

  </v-container>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'AccountPage',
  data () {
    return {
      tab: 0,
      dialog: false,
      valid: true,
      editInfoMode: false,
      infoName: '',
      infoEmail: '',
      passwordConfirm: '',
      passwordCurrent: '',
      passwordNew: '',
      invalidPasswordError: false,
      editPasswordMode: false,
      currentPasswordRules: [
        v => !!v || this.$t('passwordEmptyError'),
        v => !this.invalidPasswordError || this.$t('passwordInvalidError')
      ],
      emailRules: [
        v => this.$validateEmail(v) || this.$t('emailError')
      ],
      passwordRules: [
        v => !!v || this.$t('passwordEmptyError'),
        v => v.length >= 8 || this.$t('passwordLengthError'),
        v => v !== this.passwordCurrent || this.$t('passwordNotDifferentError')
      ],
      confirmPasswordRules: [
        v => this.passwordConfirm === this.passwordNew || this.$t('passwordMatchError')
      ],
      testId: 0
    }
  },
  computed: {
    ...mapGetters(['tabsDark', 'tabsColor', 'tabsSliderColor', 'user'])
  },
  methods: {
    deleteAccount () {
      this.dialog = false
      this.$http.delete('/api/users/current').then(response => {
        this.$http.post('/api/authentication/logout').then(response => {
          this.$router.push('/login')
          this.$socket.close()
        })
      })
    },
    clearCurrentPasswordErrors () {
      this.invalidPasswordError = false
    },
    submitInfo () {
      if (!this.$refs.emailField.validate()) return

      // Axios request to submit info (username, name, email)
      this.$http.post('/api/users/current', { name: this.infoName, email: this.infoEmail }).then(response => {
        this.editInfoMode = false
        this.$store.dispatch('loadUser')
        this.$snackbar({ color: 'success', text: this.$t('userInfoChanged') })
      })
    },
    submitPassword () {
      if (this.$refs.passwordForm.validate()) {
        this.$http.post('/api/users/' + this.user.id + '/password', { passwordCurrent: this.passwordCurrent, password: this.passwordNew }).then(response => {
          this.cancelEditPassword()
          this.$snackbar({ color: 'success', text: this.$t('passwordChanged') })
        }).catch(error => {
          if (error.response.data.error === 'PASSWORD_INVALID') {
            this.invalidPasswordError = true
            this.passwordNew = ''
            this.passwordConfirm = ''
          }
          this.$refs.passwordForm.validate()
        })
      }
    },
    editInfo () {
      this.infoName = this.user.name
      this.infoEmail = this.user.email
      this.editInfoMode = true
    },
    editPassword () {
      this.editPasswordMode = true
    },
    cancelEditInfo () {
      this.editInfoMode = false
    },
    cancelEditPassword () {
      this.editPasswordMode = false
      this.passwordCurrent = ''
      this.passwordNew = ''
      this.passwordConfirm = ''
    },
    newAlert () {
      this.$alert({ type: 'success', text: 'aaaaaaaaaaaaaaaaaaaaaaaaaas das dsad asdpioipoidijoijoawiojdiojijowaijo d o wiadijo oiawio jdi aaaaaaaaaaaaaaaaaaaaaa uo iashdoiuas dhuas hduioash diuash diuash diuash diuh test ' + this.testId })
      this.testId++
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.delete-button {
  margin-top: 40px;
}

.info-buttons {
  margin: 0;
}
.info-input {
  margin: 20px;
}
.info-heading {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.info-heading > span {
  margin-left: 10px;
}
.info-text {
  margin-left: 34px;
  font-family: 'Roboto Mono';
}
</style>