juice-shop
98 строк · 4.1 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { SecurityAnswerService } from '../Services/security-answer.service'
7import { UserService } from '../Services/user.service'
8import { type AbstractControl, UntypedFormControl, Validators } from '@angular/forms'
9import { Component, NgZone, type OnInit } from '@angular/core'
10import { SecurityQuestionService } from '../Services/security-question.service'
11import { Router } from '@angular/router'
12import { library } from '@fortawesome/fontawesome-svg-core'
13import { MatSnackBar } from '@angular/material/snack-bar'
14
15import { faExclamationCircle, faUserPlus } from '@fortawesome/free-solid-svg-icons'
16import { FormSubmitService } from '../Services/form-submit.service'
17import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
18import { TranslateService } from '@ngx-translate/core'
19import { type SecurityQuestion } from '../Models/securityQuestion.model'
20
21library.add(faUserPlus, faExclamationCircle)
22
23@Component({
24selector: 'app-register',
25templateUrl: './register.component.html',
26styleUrls: ['./register.component.scss']
27})
28export class RegisterComponent implements OnInit {
29public emailControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.email])
30public passwordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(40)])
31public repeatPasswordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, matchValidator(this.passwordControl)])
32public securityQuestionControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
33public securityAnswerControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
34public securityQuestions!: SecurityQuestion[]
35public selected?: number
36public error: string | null = null
37
38constructor (private readonly securityQuestionService: SecurityQuestionService,
39private readonly userService: UserService,
40private readonly securityAnswerService: SecurityAnswerService,
41private readonly router: Router,
42private readonly formSubmitService: FormSubmitService,
43private readonly translateService: TranslateService,
44private readonly snackBar: MatSnackBar,
45private readonly snackBarHelperService: SnackBarHelperService,
46private readonly ngZone: NgZone) { }
47
48ngOnInit () {
49this.securityQuestionService.find(null).subscribe((securityQuestions: any) => {
50this.securityQuestions = securityQuestions
51}, (err) => { console.log(err) })
52
53this.formSubmitService.attachEnterKeyHandler('registration-form', 'registerButton', () => { this.save() })
54}
55
56save () {
57const user = {
58email: this.emailControl.value,
59password: this.passwordControl.value,
60passwordRepeat: this.repeatPasswordControl.value,
61securityQuestion: this.securityQuestions.find((question) => question.id === this.securityQuestionControl.value),
62securityAnswer: this.securityAnswerControl.value
63}
64
65this.userService.save(user).subscribe((response: any) => {
66this.securityAnswerService.save({
67UserId: response.id,
68answer: this.securityAnswerControl.value,
69SecurityQuestionId: this.securityQuestionControl.value
70}).subscribe(() => {
71this.ngZone.run(async () => await this.router.navigate(['/login']))
72this.snackBarHelperService.open('CONFIRM_REGISTER')
73})
74}, (err) => {
75console.log(err)
76if (err.error?.errors) {
77const error = err.error.errors[0]
78if (error.message) {
79// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
80this.error = error.message[0].toUpperCase() + error.message.slice(1)
81} else {
82this.error = error
83}
84}
85})
86}
87}
88
89function matchValidator (passwordControl: AbstractControl) {
90return function matchOtherValidate (repeatPasswordControl: UntypedFormControl) {
91const password = passwordControl.value
92const passwordRepeat = repeatPasswordControl.value
93if (password !== passwordRepeat) {
94return { notSame: true }
95}
96return null
97}
98}
99