]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-settings/my-account-change-password/my-account-change-password.component.ts
Increase global font size
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-change-password / my-account-change-password.component.ts
1 import { filter } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { AuthService, Notifier, UserService } from '@app/core'
4 import {
5 USER_CONFIRM_PASSWORD_VALIDATOR,
6 USER_EXISTING_PASSWORD_VALIDATOR,
7 USER_PASSWORD_VALIDATOR
8 } from '@app/shared/form-validators/user-validators'
9 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
10 import { User } from '@shared/models'
11
12 @Component({
13 selector: 'my-account-change-password',
14 templateUrl: './my-account-change-password.component.html',
15 styleUrls: [ './my-account-change-password.component.scss' ]
16 })
17 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
18 error: string = null
19 user: User = null
20
21 constructor (
22 protected formValidatorService: FormValidatorService,
23 private notifier: Notifier,
24 private authService: AuthService,
25 private userService: UserService
26 ) {
27 super()
28 }
29
30 ngOnInit () {
31 this.buildForm({
32 'current-password': USER_EXISTING_PASSWORD_VALIDATOR,
33 'new-password': USER_PASSWORD_VALIDATOR,
34 'new-confirmed-password': USER_CONFIRM_PASSWORD_VALIDATOR
35 })
36
37 this.user = this.authService.getUser()
38
39 const confirmPasswordControl = this.form.get('new-confirmed-password')
40
41 confirmPasswordControl.valueChanges
42 .pipe(filter(v => v !== this.form.value['new-password']))
43 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
44 }
45
46 changePassword () {
47 const currentPassword = this.form.value['current-password']
48 const newPassword = this.form.value['new-password']
49
50 this.userService.changePassword(currentPassword, newPassword)
51 .subscribe({
52 next: () => {
53 this.notifier.success($localize`Password updated.`)
54
55 this.form.reset()
56 this.error = null
57 },
58
59 error: err => {
60 if (err.status === 401) {
61 this.error = $localize`You current password is invalid.`
62 return
63 }
64
65 this.error = err.message
66 }
67 })
68 }
69 }