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