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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
import { Component, OnInit } from '@angular/core';
import { AuthService } from 'src/app/_services/auth.service';
@Component({
selector: 'app-register-modal',
templateUrl: './register-modal.component.html',
styleUrls: ['./register-modal.component.css']
})
export class RegisterModalComponent implements OnInit {
firstName: string = '';
lastName: string = '';
username: string = '';
email: string = '';
pass1: string = '';
pass2: string = '';
wrongFirstNameBool: boolean = false;
wrongLastNameBool: boolean = false;
wrongUsernameBool: boolean = false;
wrongEmailBool: boolean = false;
wrongPass1Bool: boolean = false;
wrongPass2Bool: boolean = false;
pattName: RegExp = /^[a-zA-ZšŠđĐčČćĆžŽ]+([ \-][a-zA-ZšŠđĐčČćĆžŽ]+)*$/;
pattUsername: RegExp = /^[a-zA-Z0-9]{6,18}$/;
pattTwoSpaces: RegExp = / /;
pattEmail: RegExp = /^[a-zA-Z0-9]+([\.\-\+][a-zA-Z0-9]+)*\@([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}$/;
pattPassword: RegExp = /.{6,30}$/;
constructor(
private authService: AuthService
) { }
ngOnInit(): void {
}
doRegister() {
this.validation();
}
resetData() {
this.firstName = this.lastName = this.username = this.email = this.pass1 = this.pass2 = '';
this.wrongFirstNameBool = this.wrongLastNameBool = this.wrongUsernameBool = this.wrongEmailBool = this.wrongPass1Bool = this.wrongPass2Bool = false;
}
isCorrectName(element: string): boolean {
if (this.pattName.test(element) && !(this.pattTwoSpaces.test(element)) && (element.length >= 1 && element.length <= 30))
return true;
return false;
}
isCorrectUsername(element: string): boolean {
if (this.pattUsername.test(element) && !(this.pattTwoSpaces.test(element)) && (element.length >= 1 && element.length <= 30))
return true;
return false;
}
isCorrectEmail(element: string): boolean {
if (this.pattEmail.test(element.toLowerCase()) && element.length <= 320)
return true;
return false;
}
isCorrectPassword(element: string): boolean {
if (this.pattPassword.test(element))
return true;
return false;
}
firstNameValidation() {
if (this.isCorrectName(this.firstName) == true) {
this.wrongFirstNameBool = false;
return;
}
(<HTMLSelectElement>document.getElementById('firstName')).focus();
this.wrongFirstNameBool = true;
}
lastNameValidation() {
if (this.isCorrectName(this.lastName) == true) {
this.wrongLastNameBool = false;
return;
}
(<HTMLSelectElement>document.getElementById('lastName')).focus();
this.wrongLastNameBool = true;
}
usernameValidation() {
if (this.isCorrectUsername(this.username) == true) {
this.wrongUsernameBool = false;
return;
}
(<HTMLSelectElement>document.getElementById('username-register')).focus();
this.wrongUsernameBool = true;
}
emailValidation() {
if (this.isCorrectEmail(this.email) == true) {
this.wrongEmailBool = false;
return;
}
(<HTMLSelectElement>document.getElementById('email')).focus();
this.wrongEmailBool = true;
}
passwordValidation() {
if (this.isCorrectPassword(this.pass1) && this.isCorrectPassword(this.pass2) && this.pass1 == this.pass2) {
this.wrongPass1Bool = false;
this.wrongPass2Bool = false;
return;
}
this.pass1 = ''; //brisi obe ukucane lozinke
this.pass2 = '';
(<HTMLSelectElement>document.getElementById('pass1')).focus();
this.wrongPass1Bool = true;
this.wrongPass2Bool = true;
}
validation() {
this.firstName = this.firstName.trim();
this.lastName = this.lastName.trim();
this.username = this.username.trim();
this.email = this.email.trim();
this.firstNameValidation();
this.lastNameValidation();
this.usernameValidation();
this.emailValidation();
this.passwordValidation();
if (!(this.wrongFirstNameBool || this.wrongLastNameBool || this.wrongUsernameBool ||
this.wrongEmailBool || this.wrongPass1Bool || this.wrongPass2Bool)) { //sve ok, registruj ga
let user = {
firstName: this.firstName,
lastName: this.lastName,
username: this.username,
password: this.pass1,
email: this.email
}
this.authService.register(user)
.subscribe(
(response) => {
console.log(response);
if (response == 'User added') {
//nakon sto je registrovan, nek bude ulogovan
this.authService.login(this.username, this.pass1).subscribe((response) => {
this.authService.authenticate(response);
console.log("close button");
(<HTMLSelectElement>document.getElementById('closeButtonReg')).click();
//(<HTMLSelectElement>document.getElementById('linkToLoginModal')).click();
}, (error) => console.warn(error));
}
else if (response == 'Email Already Exists') {
alert('Nalog sa unetim email-om već postoji!');
(<HTMLSelectElement>document.getElementById('email')).focus();
}
else if (response == 'Username Already Exists') {
alert('Nalog sa unetim korisničkim imenom već postoji!');
(<HTMLSelectElement>document.getElementById('username-register')).focus();
}
}
);
}
}
}
|