ciyidogan commited on
Commit
d90ef2e
·
verified ·
1 Parent(s): 9b053d5

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
- @Component({
6
- selector: 'app-user-info',
7
- standalone: true,
8
- imports: [CommonModule, FormsModule],
9
- template: `
10
- <div class="user-info-container">
11
- <h2>User Information</h2>
12
-
13
- <div class="card">
14
- <div class="card-body">
15
- <p class="text-muted">Password change functionality coming soon...</p>
16
-
17
- <form (ngSubmit)="changePassword()" #passwordForm="ngForm">
18
- <div class="form-group">
19
- <label for="currentPassword">Current Password</label>
20
- <input
21
- type="password"
22
- id="currentPassword"
23
- name="currentPassword"
24
- [(ngModel)]="currentPassword"
25
- required
26
- disabled
27
- >
28
- </div>
29
-
30
- <div class="form-group">
31
- <label for="newPassword">New Password</label>
32
- <input
33
- type="password"
34
- id="newPassword"
35
- name="newPassword"
36
- [(ngModel)]="newPassword"
37
- required
38
- disabled
39
- >
40
- </div>
41
-
42
- <div class="form-group">
43
- <label for="confirmPassword">Confirm New Password</label>
44
- <input
45
- type="password"
46
- id="confirmPassword"
47
- name="confirmPassword"
48
- [(ngModel)]="confirmPassword"
49
- required
50
- disabled
51
- >
52
- </div>
53
-
54
- <button type="submit" class="btn btn-primary" disabled>
55
- Change Password
56
- </button>
57
- </form>
58
- </div>
59
- </div>
60
- </div>
61
- `,
62
- styles: [`
63
- .user-info-container {
64
- h2 {
65
- margin-bottom: 1.5rem;
66
- }
67
- }
68
-
69
- .text-muted {
70
- color: #6c757d;
71
- margin-bottom: 1rem;
72
- }
73
- `]
74
- })
75
- export class UserInfoComponent {
76
- currentPassword = '';
77
- newPassword = '';
78
- confirmPassword = '';
79
-
80
- changePassword() {
81
- console.log('Password change not implemented yet');
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
  }