]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/reset-password/reset-password.component.ts
Form validators refractoring
[github/Chocobozzz/PeerTube.git] / client / src / app / reset-password / reset-password.component.ts
CommitLineData
ecb4e35f 1import { Component, OnInit } from '@angular/core'
ecb4e35f
C
2import { ActivatedRoute, Router } from '@angular/router'
3import { USER_PASSWORD, UserService } from '@app/shared'
4import { NotificationsService } from 'angular2-notifications'
5import { AuthService } from '../core'
6import { FormReactive } from '../shared'
b1d40cff 7import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478
C
8import { RESET_PASSWORD_CONFIRM } from '@app/shared/forms/form-validators/reset-password'
9import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
ecb4e35f
C
10
11@Component({
12 selector: 'my-login',
13 templateUrl: './reset-password.component.html',
14 styleUrls: [ './reset-password.component.scss' ]
15})
16
17export class ResetPasswordComponent extends FormReactive implements OnInit {
ecb4e35f
C
18 private userId: number
19 private verificationString: string
20
21 constructor (
d18d6478 22 protected formValidatorService: FormValidatorService,
ecb4e35f
C
23 private authService: AuthService,
24 private userService: UserService,
25 private notificationsService: NotificationsService,
ecb4e35f 26 private router: Router,
b1d40cff
C
27 private route: ActivatedRoute,
28 private i18n: I18n
ecb4e35f
C
29 ) {
30 super()
31 }
32
ecb4e35f 33 ngOnInit () {
d18d6478
C
34 this.buildForm({
35 password: USER_PASSWORD,
36 'password-confirm': RESET_PASSWORD_CONFIRM
37 })
ecb4e35f
C
38
39 this.userId = this.route.snapshot.queryParams['userId']
40 this.verificationString = this.route.snapshot.queryParams['verificationString']
41
42 if (!this.userId || !this.verificationString) {
b1d40cff 43 this.notificationsService.error(this.i18n('Error'), this.i18n('Unable to find user id or verification string.'))
ecb4e35f
C
44 this.router.navigate([ '/' ])
45 }
46 }
47
48 resetPassword () {
49 this.userService.resetPassword(this.userId, this.verificationString, this.form.value.password)
50 .subscribe(
51 () => {
b1d40cff 52 this.notificationsService.success(this.i18n('Success'), this.i18n('Your password has been successfully reset!'))
ecb4e35f
C
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}