aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/reset-password/reset-password.component.ts
blob: cb4a634f13701dea6cdd635c139cb18ada5a359d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Component, OnInit } from '@angular/core'
import { ActivatedRoute, Router } from '@angular/router'
import { UserService, UserValidatorsService } from '@app/shared'
import { NotificationsService } from 'angular2-notifications'
import { AuthService } from '../core'
import { FormReactive } from '../shared'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
import { ResetPasswordValidatorsService } from '@app/shared/forms/form-validators/reset-password-validators.service'

@Component({
  selector: 'my-login',
  templateUrl: './reset-password.component.html',
  styleUrls: [ './reset-password.component.scss' ]
})

export class ResetPasswordComponent extends FormReactive implements OnInit {
  private userId: number
  private verificationString: string

  constructor (
    protected formValidatorService: FormValidatorService,
    private resetPasswordValidatorsService: ResetPasswordValidatorsService,
    private userValidatorsService: UserValidatorsService,
    private authService: AuthService,
    private userService: UserService,
    private notificationsService: NotificationsService,
    private router: Router,
    private route: ActivatedRoute,
    private i18n: I18n
  ) {
    super()
  }

  ngOnInit () {
    this.buildForm({
      password: this.userValidatorsService.USER_PASSWORD,
      'password-confirm': this.resetPasswordValidatorsService.RESET_PASSWORD_CONFIRM
    })

    this.userId = this.route.snapshot.queryParams['userId']
    this.verificationString = this.route.snapshot.queryParams['verificationString']

    if (!this.userId || !this.verificationString) {
      this.notificationsService.error(this.i18n('Error'), this.i18n('Unable to find user id or verification string.'))
      this.router.navigate([ '/' ])
    }
  }

  resetPassword () {
    this.userService.resetPassword(this.userId, this.verificationString, this.form.value.password)
      .subscribe(
        () => {
          this.notificationsService.success(this.i18n('Success'), this.i18n('Your password has been successfully reset!'))
          this.router.navigate([ '/login' ])
        },

        err => this.notificationsService.error('Error', err.message)
      )
  }

  isConfirmedPasswordValid () {
    const values = this.form.value
    return values.password === values['password-confirm']
  }
}