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,109 @@
|
|
1 |
-
import { Component } from '@angular/core';
|
2 |
-
import { CommonModule } from '@angular/common';
|
3 |
-
import { FormsModule } from '@angular/forms';
|
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, inject } from '@angular/core';
|
2 |
+
import { CommonModule } from '@angular/common';
|
3 |
+
import { FormsModule } from '@angular/forms';
|
4 |
+
import { MatFormFieldModule } from '@angular/material/form-field';
|
5 |
+
import { MatInputModule } from '@angular/material/input';
|
6 |
+
import { MatButtonModule } from '@angular/material/button';
|
7 |
+
import { MatIconModule } from '@angular/material/icon';
|
8 |
+
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
9 |
+
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
10 |
+
import { ApiService } from '../../services/api.service';
|
11 |
+
import { AuthService } from '../../services/auth.service';
|
12 |
+
|
13 |
+
@Component({
|
14 |
+
selector: 'app-user-info',
|
15 |
+
standalone: true,
|
16 |
+
imports: [
|
17 |
+
CommonModule,
|
18 |
+
FormsModule,
|
19 |
+
MatFormFieldModule,
|
20 |
+
MatInputModule,
|
21 |
+
MatButtonModule,
|
22 |
+
MatIconModule,
|
23 |
+
MatProgressBarModule,
|
24 |
+
MatSnackBarModule
|
25 |
+
],
|
26 |
+
templateUrl: './user-info.component.html',
|
27 |
+
styleUrls: ['./user-info.component.scss']
|
28 |
+
})
|
29 |
+
export class UserInfoComponent {
|
30 |
+
private apiService = inject(ApiService);
|
31 |
+
private authService = inject(AuthService);
|
32 |
+
private snackBar = inject(MatSnackBar);
|
33 |
+
|
34 |
+
username = this.authService.getUsername() || '';
|
35 |
+
currentPassword = '';
|
36 |
+
newPassword = '';
|
37 |
+
confirmPassword = '';
|
38 |
+
saving = false;
|
39 |
+
showCurrentPassword = false;
|
40 |
+
showNewPassword = false;
|
41 |
+
showConfirmPassword = false;
|
42 |
+
|
43 |
+
get passwordStrength(): { level: number; text: string; color: string } {
|
44 |
+
if (!this.newPassword) {
|
45 |
+
return { level: 0, text: '', color: '' };
|
46 |
+
}
|
47 |
+
|
48 |
+
let strength = 0;
|
49 |
+
|
50 |
+
// Length check
|
51 |
+
if (this.newPassword.length >= 8) strength++;
|
52 |
+
if (this.newPassword.length >= 12) strength++;
|
53 |
+
|
54 |
+
// Character variety
|
55 |
+
if (/[a-z]/.test(this.newPassword)) strength++;
|
56 |
+
if (/[A-Z]/.test(this.newPassword)) strength++;
|
57 |
+
if (/[0-9]/.test(this.newPassword)) strength++;
|
58 |
+
if (/[^a-zA-Z0-9]/.test(this.newPassword)) strength++;
|
59 |
+
|
60 |
+
if (strength <= 2) {
|
61 |
+
return { level: 33, text: 'Weak', color: 'warn' };
|
62 |
+
} else if (strength <= 4) {
|
63 |
+
return { level: 66, text: 'Medium', color: 'accent' };
|
64 |
+
} else {
|
65 |
+
return { level: 100, text: 'Strong', color: 'primary' };
|
66 |
+
}
|
67 |
+
}
|
68 |
+
|
69 |
+
get isFormValid(): boolean {
|
70 |
+
return !!(
|
71 |
+
this.currentPassword &&
|
72 |
+
this.newPassword &&
|
73 |
+
this.confirmPassword &&
|
74 |
+
this.newPassword === this.confirmPassword &&
|
75 |
+
this.newPassword.length >= 8 &&
|
76 |
+
/[a-z]/.test(this.newPassword) &&
|
77 |
+
/[A-Z]/.test(this.newPassword) &&
|
78 |
+
/[0-9]/.test(this.newPassword)
|
79 |
+
);
|
80 |
+
}
|
81 |
+
|
82 |
+
async changePassword() {
|
83 |
+
if (!this.isFormValid) {
|
84 |
+
this.snackBar.open('Please fill all fields correctly', 'Close', { duration: 3000 });
|
85 |
+
return;
|
86 |
+
}
|
87 |
+
|
88 |
+
if (this.newPassword !== this.confirmPassword) {
|
89 |
+
this.snackBar.open('New passwords do not match', 'Close', { duration: 3000 });
|
90 |
+
return;
|
91 |
+
}
|
92 |
+
|
93 |
+
this.saving = true;
|
94 |
+
try {
|
95 |
+
await this.apiService.changePassword(this.currentPassword, this.newPassword).toPromise();
|
96 |
+
this.snackBar.open('Password changed successfully', 'Close', { duration: 3000 });
|
97 |
+
|
98 |
+
// Clear form
|
99 |
+
this.currentPassword = '';
|
100 |
+
this.newPassword = '';
|
101 |
+
this.confirmPassword = '';
|
102 |
+
} catch (error: any) {
|
103 |
+
const message = error.error?.detail || 'Failed to change password';
|
104 |
+
this.snackBar.open(message, 'Close', { duration: 5000 });
|
105 |
+
} finally {
|
106 |
+
this.saving = false;
|
107 |
+
}
|
108 |
+
}
|
109 |
}
|