Merge from upstream
[oweals/peertube.git] / client / src / app / reset-password / reset-password.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { UserService, UserValidatorsService } from '@app/shared'
4 import { NotificationsService } from 'angular2-notifications'
5 import { FormReactive } from '../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 import { ResetPasswordValidatorsService } from '@app/shared/forms/form-validators/reset-password-validators.service'
9
10 @Component({
11   selector: 'my-login',
12   templateUrl: './reset-password.component.html',
13   styleUrls: [ './reset-password.component.scss' ]
14 })
15
16 export class ResetPasswordComponent extends FormReactive implements OnInit {
17   private userId: number
18   private verificationString: string
19
20   constructor (
21     protected formValidatorService: FormValidatorService,
22     private resetPasswordValidatorsService: ResetPasswordValidatorsService,
23     private userValidatorsService: UserValidatorsService,
24     private userService: UserService,
25     private notificationsService: NotificationsService,
26     private router: Router,
27     private route: ActivatedRoute,
28     private i18n: I18n
29   ) {
30     super()
31   }
32
33   ngOnInit () {
34     this.buildForm({
35       password: this.userValidatorsService.USER_PASSWORD,
36       'password-confirm': this.resetPasswordValidatorsService.RESET_PASSWORD_CONFIRM
37     })
38
39     this.userId = this.route.snapshot.queryParams['userId']
40     this.verificationString = this.route.snapshot.queryParams['verificationString']
41
42     if (!this.userId || !this.verificationString) {
43       this.notificationsService.error(this.i18n('Error'), this.i18n('Unable to find user id or verification string.'))
44       this.router.navigate([ '/' ])
45     }
46   }
47
48   resetPassword () {
49     this.userService.resetPassword(this.userId, this.verificationString, this.form.value.password)
50       .subscribe(
51         () => {
52           this.notificationsService.success(this.i18n('Success'), this.i18n('Your password has been successfully reset!'))
53           this.router.navigate([ '/login' ])
54         },
55
56         err => this.notificationsService.error('Error', err.message)
57       )
58   }
59
60   isConfirmedPasswordValid () {
61     const values = this.form.value
62     return values.password === values['password-confirm']
63   }
64 }