juice-shop

Форк
0
/
register.component.ts 
98 строк · 4.1 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { SecurityAnswerService } from '../Services/security-answer.service'
7
import { UserService } from '../Services/user.service'
8
import { type AbstractControl, UntypedFormControl, Validators } from '@angular/forms'
9
import { Component, NgZone, type OnInit } from '@angular/core'
10
import { SecurityQuestionService } from '../Services/security-question.service'
11
import { Router } from '@angular/router'
12
import { library } from '@fortawesome/fontawesome-svg-core'
13
import { MatSnackBar } from '@angular/material/snack-bar'
14

15
import { faExclamationCircle, faUserPlus } from '@fortawesome/free-solid-svg-icons'
16
import { FormSubmitService } from '../Services/form-submit.service'
17
import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
18
import { TranslateService } from '@ngx-translate/core'
19
import { type SecurityQuestion } from '../Models/securityQuestion.model'
20

21
library.add(faUserPlus, faExclamationCircle)
22

23
@Component({
24
  selector: 'app-register',
25
  templateUrl: './register.component.html',
26
  styleUrls: ['./register.component.scss']
27
})
28
export class RegisterComponent implements OnInit {
29
  public emailControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.email])
30
  public passwordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(40)])
31
  public repeatPasswordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, matchValidator(this.passwordControl)])
32
  public securityQuestionControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
33
  public securityAnswerControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
34
  public securityQuestions!: SecurityQuestion[]
35
  public selected?: number
36
  public error: string | null = null
37

38
  constructor (private readonly securityQuestionService: SecurityQuestionService,
39
    private readonly userService: UserService,
40
    private readonly securityAnswerService: SecurityAnswerService,
41
    private readonly router: Router,
42
    private readonly formSubmitService: FormSubmitService,
43
    private readonly translateService: TranslateService,
44
    private readonly snackBar: MatSnackBar,
45
    private readonly snackBarHelperService: SnackBarHelperService,
46
    private readonly ngZone: NgZone) { }
47

48
  ngOnInit () {
49
    this.securityQuestionService.find(null).subscribe((securityQuestions: any) => {
50
      this.securityQuestions = securityQuestions
51
    }, (err) => { console.log(err) })
52

53
    this.formSubmitService.attachEnterKeyHandler('registration-form', 'registerButton', () => { this.save() })
54
  }
55

56
  save () {
57
    const user = {
58
      email: this.emailControl.value,
59
      password: this.passwordControl.value,
60
      passwordRepeat: this.repeatPasswordControl.value,
61
      securityQuestion: this.securityQuestions.find((question) => question.id === this.securityQuestionControl.value),
62
      securityAnswer: this.securityAnswerControl.value
63
    }
64

65
    this.userService.save(user).subscribe((response: any) => {
66
      this.securityAnswerService.save({
67
        UserId: response.id,
68
        answer: this.securityAnswerControl.value,
69
        SecurityQuestionId: this.securityQuestionControl.value
70
      }).subscribe(() => {
71
        this.ngZone.run(async () => await this.router.navigate(['/login']))
72
        this.snackBarHelperService.open('CONFIRM_REGISTER')
73
      })
74
    }, (err) => {
75
      console.log(err)
76
      if (err.error?.errors) {
77
        const error = err.error.errors[0]
78
        if (error.message) {
79
          // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
80
          this.error = error.message[0].toUpperCase() + error.message.slice(1)
81
        } else {
82
          this.error = error
83
        }
84
      }
85
    })
86
  }
87
}
88

89
function matchValidator (passwordControl: AbstractControl) {
90
  return function matchOtherValidate (repeatPasswordControl: UntypedFormControl) {
91
    const password = passwordControl.value
92
    const passwordRepeat = repeatPasswordControl.value
93
    if (password !== passwordRepeat) {
94
      return { notSame: true }
95
    }
96
    return null
97
  }
98
}
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.