]> 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
Refractor notification service
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-change-password / my-account-change-password.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { AuthService, Notifier } from '@app/core'
3 import { FormReactive, UserService } from '../../../shared'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
6 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
7 import { filter } from 'rxjs/operators'
8 import { User } from '../../../../../../shared'
9
10 @Component({
11 selector: 'my-account-change-password',
12 templateUrl: './my-account-change-password.component.html',
13 styleUrls: [ './my-account-change-password.component.scss' ]
14 })
15 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
16 error: string = null
17 user: User = null
18
19 constructor (
20 protected formValidatorService: FormValidatorService,
21 private userValidatorsService: UserValidatorsService,
22 private notifier: Notifier,
23 private authService: AuthService,
24 private userService: UserService,
25 private i18n: I18n
26 ) {
27 super()
28 }
29
30 ngOnInit () {
31 this.buildForm({
32 'current-password': this.userValidatorsService.USER_PASSWORD,
33 'new-password': this.userValidatorsService.USER_PASSWORD,
34 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
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).subscribe(
51 () => {
52 this.notifier.success(this.i18n('Password updated.'))
53
54 this.form.reset()
55 this.error = null
56 },
57
58 err => {
59 if (err.status === 401) {
60 this.error = this.i18n('You current password is invalid.')
61 return
62 }
63
64 this.error = err.message
65 }
66 )
67 }
68 }