]> 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
check old password before change
[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 { NotificationsService } from 'angular2-notifications'
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 { AuthService } from '@app/core';
9 import { User } from '../../../../../../shared';
10
11 @Component({
12 selector: 'my-account-change-password',
13 templateUrl: './my-account-change-password.component.html',
14 styleUrls: [ './my-account-change-password.component.scss' ]
15 })
16 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
17 error: string = null
18 user: User = null
19
20 constructor (
21 protected formValidatorService: FormValidatorService,
22 private userValidatorsService: UserValidatorsService,
23 private notificationsService: NotificationsService,
24 private authService: AuthService,
25 private userService: UserService,
26 private i18n: I18n
27 ) {
28 super()
29 }
30
31 ngOnInit () {
32 this.buildForm({
33 'old-password': this.userValidatorsService.USER_PASSWORD,
34 'new-password': this.userValidatorsService.USER_PASSWORD,
35 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD
36 })
37
38 this.user = this.authService.getUser()
39
40 const confirmPasswordControl = this.form.get('new-confirmed-password')
41
42 confirmPasswordControl.valueChanges
43 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
44 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
45 }
46
47 checkPassword () {
48 this.error = null
49 const oldPassword = this.form.value[ 'old-password' ];
50
51 // compare old password
52 this.authService.login(this.user.account.name, oldPassword)
53 .subscribe(
54 () => this.changePassword(),
55 err => {
56 if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect old password.')
57 else this.error = err.message
58 }
59 )
60
61 }
62
63 private changePassword(){
64 this.userService.changePassword(this.form.value[ 'new-password' ]).subscribe(
65 () => {
66 this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.'))
67
68 this.form.reset()
69 },
70
71 err => this.error = err.message
72 )
73 }
74 }