Spaces:
Running
Running
Update flare-ui/src/app/components/user-info/user-info.component.ts
Browse files
flare-ui/src/app/components/user-info/user-info.component.ts
CHANGED
@@ -1,83 +1,110 @@
|
|
1 |
-
import { Component } from '@angular/core';
|
2 |
-
import {
|
3 |
-
import {
|
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 |
}
|
|
|
1 |
+
import { Component } from '@angular/core';
|
2 |
+
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
3 |
+
import { MatSnackBar } from '@angular/material/snack-bar';
|
4 |
+
import { ApiService } from '../../services/api.service';
|
5 |
+
|
6 |
+
@Component({
|
7 |
+
selector: 'app-user-info',
|
8 |
+
templateUrl: './user-info.component.html',
|
9 |
+
styleUrls: ['./user-info.component.scss']
|
10 |
+
})
|
11 |
+
export class UserInfoComponent {
|
12 |
+
passwordForm: FormGroup;
|
13 |
+
hideCurrentPassword = true;
|
14 |
+
hideNewPassword = true;
|
15 |
+
hideConfirmPassword = true;
|
16 |
+
passwordStrength = 0;
|
17 |
+
passwordStrengthText = '';
|
18 |
+
passwordStrengthColor = '';
|
19 |
+
|
20 |
+
constructor(
|
21 |
+
private fb: FormBuilder,
|
22 |
+
private apiService: ApiService,
|
23 |
+
private snackBar: MatSnackBar
|
24 |
+
) {
|
25 |
+
this.passwordForm = this.fb.group({
|
26 |
+
currentPassword: ['', Validators.required],
|
27 |
+
newPassword: ['', [
|
28 |
+
Validators.required,
|
29 |
+
Validators.minLength(8),
|
30 |
+
Validators.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/)
|
31 |
+
]],
|
32 |
+
confirmPassword: ['', Validators.required]
|
33 |
+
}, { validators: this.passwordMatchValidator });
|
34 |
+
|
35 |
+
// Watch password changes for strength indicator
|
36 |
+
this.passwordForm.get('newPassword')?.valueChanges.subscribe(password => {
|
37 |
+
this.updatePasswordStrength(password);
|
38 |
+
});
|
39 |
+
}
|
40 |
+
|
41 |
+
passwordMatchValidator(form: FormGroup) {
|
42 |
+
const newPassword = form.get('newPassword');
|
43 |
+
const confirmPassword = form.get('confirmPassword');
|
44 |
+
|
45 |
+
if (newPassword && confirmPassword && newPassword.value !== confirmPassword.value) {
|
46 |
+
confirmPassword.setErrors({ passwordMismatch: true });
|
47 |
+
} else {
|
48 |
+
confirmPassword.setErrors(null);
|
49 |
+
}
|
50 |
+
return null;
|
51 |
+
}
|
52 |
+
|
53 |
+
updatePasswordStrength(password: string) {
|
54 |
+
if (!password) {
|
55 |
+
this.passwordStrength = 0;
|
56 |
+
this.passwordStrengthText = '';
|
57 |
+
return;
|
58 |
+
}
|
59 |
+
|
60 |
+
let strength = 0;
|
61 |
+
|
62 |
+
// Length check
|
63 |
+
if (password.length >= 8) strength += 20;
|
64 |
+
if (password.length >= 12) strength += 20;
|
65 |
+
|
66 |
+
// Character variety
|
67 |
+
if (/[a-z]/.test(password)) strength += 20;
|
68 |
+
if (/[A-Z]/.test(password)) strength += 20;
|
69 |
+
if (/\d/.test(password)) strength += 20;
|
70 |
+
if (/[^a-zA-Z0-9]/.test(password)) strength += 20;
|
71 |
+
|
72 |
+
this.passwordStrength = Math.min(strength, 100);
|
73 |
+
|
74 |
+
if (this.passwordStrength < 40) {
|
75 |
+
this.passwordStrengthText = 'Weak';
|
76 |
+
this.passwordStrengthColor = 'warn';
|
77 |
+
} else if (this.passwordStrength < 70) {
|
78 |
+
this.passwordStrengthText = 'Medium';
|
79 |
+
this.passwordStrengthColor = 'accent';
|
80 |
+
} else {
|
81 |
+
this.passwordStrengthText = 'Strong';
|
82 |
+
this.passwordStrengthColor = 'primary';
|
83 |
+
}
|
84 |
+
}
|
85 |
+
|
86 |
+
async onSubmit() {
|
87 |
+
if (this.passwordForm.invalid) {
|
88 |
+
return;
|
89 |
+
}
|
90 |
+
|
91 |
+
try {
|
92 |
+
await this.apiService.changePassword(
|
93 |
+
this.passwordForm.value.currentPassword,
|
94 |
+
this.passwordForm.value.newPassword
|
95 |
+
).toPromise();
|
96 |
+
|
97 |
+
this.snackBar.open('Password changed successfully', 'Close', {
|
98 |
+
duration: 3000,
|
99 |
+
panelClass: 'success-snackbar'
|
100 |
+
});
|
101 |
+
|
102 |
+
this.passwordForm.reset();
|
103 |
+
} catch (error) {
|
104 |
+
this.snackBar.open('Failed to change password', 'Close', {
|
105 |
+
duration: 5000,
|
106 |
+
panelClass: 'error-snackbar'
|
107 |
+
});
|
108 |
+
}
|
109 |
+
}
|
110 |
}
|