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