]> 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
Check current password on server side
[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'
df98563e 2import { NotificationsService } from 'angular2-notifications'
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
B
8import { AuthService } from '@app/core'
9import { User } from '../../../../../../shared'
af5e743b
C
10
11@Component({
12 selector: 'my-account-change-password',
4bb6886d
C
13 templateUrl: './my-account-change-password.component.html',
14 styleUrls: [ './my-account-change-password.component.scss' ]
af5e743b 15})
4bb6886d 16export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
df98563e 17 error: string = null
3805ce3f 18 user: User = null
af5e743b 19
df98563e 20 constructor (
d18d6478 21 protected formValidatorService: FormValidatorService,
e309822b 22 private userValidatorsService: UserValidatorsService,
af5e743b 23 private notificationsService: NotificationsService,
3805ce3f 24 private authService: AuthService,
b1d40cff
C
25 private userService: UserService,
26 private i18n: I18n
af5e743b 27 ) {
df98563e 28 super()
af5e743b
C
29 }
30
df98563e 31 ngOnInit () {
d18d6478 32 this.buildForm({
a890d1e0 33 'current-password': this.userValidatorsService.USER_PASSWORD,
e309822b 34 'new-password': this.userValidatorsService.USER_PASSWORD,
b0ee41df 35 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
d18d6478 36 })
af5e743b 37
3805ce3f
B
38 this.user = this.authService.getUser()
39
b0ee41df 40 const confirmPasswordControl = this.form.get('new-confirmed-password')
af5e743b 41
b0ee41df
C
42 confirmPasswordControl.valueChanges
43 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
44 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
a94419a6
J
45 }
46
a890d1e0
C
47 changePassword () {
48 const currentPassword = this.form.value[ 'current-password' ]
49 const newPassword = this.form.value[ 'new-password' ]
3805ce3f 50
a890d1e0 51 this.userService.changePassword(currentPassword, newPassword).subscribe(
b0ee41df
C
52 () => {
53 this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.'))
af5e743b 54
b0ee41df 55 this.form.reset()
a890d1e0 56 this.error = null
b0ee41df 57 },
af5e743b 58
a890d1e0
C
59 err => {
60 if (err.status === 401) {
61 this.error = this.i18n('You current password is invalid.')
62 return
63 }
64
65 this.error = err.message
66 }
df98563e 67 )
af5e743b
C
68 }
69}