]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-settings/my-account-change-password/my-account-change-password.component.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-change-password / my-account-change-password.component.ts
CommitLineData
df98563e 1import { Component, OnInit } from '@angular/core'
f8b2c1b4 2import { AuthService, Notifier } from '@app/core'
e309822b 3import { FormReactive, UserService } from '../../../shared'
b1d40cff 4import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 5import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 6import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
b0ee41df 7import { filter } from 'rxjs/operators'
be1206bb 8import { User } from '../../../../../../shared'
af5e743b
C
9
10@Component({
11 selector: 'my-account-change-password',
4bb6886d
C
12 templateUrl: './my-account-change-password.component.html',
13 styleUrls: [ './my-account-change-password.component.scss' ]
af5e743b 14})
4bb6886d 15export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
df98563e 16 error: string = null
3805ce3f 17 user: User = null
af5e743b 18
df98563e 19 constructor (
d18d6478 20 protected formValidatorService: FormValidatorService,
e309822b 21 private userValidatorsService: UserValidatorsService,
f8b2c1b4 22 private notifier: Notifier,
3805ce3f 23 private authService: AuthService,
b1d40cff
C
24 private userService: UserService,
25 private i18n: I18n
af5e743b 26 ) {
df98563e 27 super()
af5e743b
C
28 }
29
df98563e 30 ngOnInit () {
d18d6478 31 this.buildForm({
a890d1e0 32 'current-password': this.userValidatorsService.USER_PASSWORD,
e309822b 33 'new-password': this.userValidatorsService.USER_PASSWORD,
b0ee41df 34 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
d18d6478 35 })
af5e743b 36
3805ce3f
B
37 this.user = this.authService.getUser()
38
b0ee41df 39 const confirmPasswordControl = this.form.get('new-confirmed-password')
af5e743b 40
b0ee41df
C
41 confirmPasswordControl.valueChanges
42 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
43 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
a94419a6
J
44 }
45
a890d1e0
C
46 changePassword () {
47 const currentPassword = this.form.value[ 'current-password' ]
48 const newPassword = this.form.value[ 'new-password' ]
3805ce3f 49
a890d1e0 50 this.userService.changePassword(currentPassword, newPassword).subscribe(
b0ee41df 51 () => {
f8b2c1b4 52 this.notifier.success(this.i18n('Password updated.'))
af5e743b 53
b0ee41df 54 this.form.reset()
a890d1e0 55 this.error = null
b0ee41df 56 },
af5e743b 57
a890d1e0
C
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 }
df98563e 66 )
af5e743b
C
67 }
68}