ciyidogan commited on
Commit
d6fd450
·
verified ·
1 Parent(s): f655fab

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 { 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 } 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
  }