File size: 3,534 Bytes
d90ef2e
 
 
 
 
 
 
 
039c45d
d90ef2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
039c45d
d90ef2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a224f7
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
import { Component, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatCardModule } from '@angular/material/card';  // ADD THIS
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { ApiService } from '../../services/api.service';
import { AuthService } from '../../services/auth.service';

@Component({
  selector: 'app-user-info',
  standalone: true,
  imports: [
    CommonModule,
    FormsModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    MatIconModule,
    MatProgressBarModule,
    MatCardModule,  // ADD THIS
    MatSnackBarModule
  ],
  templateUrl: './user-info.component.html',
  styleUrls: ['./user-info.component.scss']
})
export class UserInfoComponent {
  private apiService = inject(ApiService);
  private authService = inject(AuthService);
  private snackBar = inject(MatSnackBar);

  username = this.authService.getUsername() || '';
  currentPassword = '';
  newPassword = '';
  confirmPassword = '';
  saving = false;
  showCurrentPassword = false;
  showNewPassword = false;
  showConfirmPassword = false;

  get passwordStrength(): { level: number; text: string; color: string } {
    if (!this.newPassword) {
      return { level: 0, text: '', color: '' };
    }

    let strength = 0;
    
    // Length check
    if (this.newPassword.length >= 8) strength++;
    if (this.newPassword.length >= 12) strength++;
    
    // Character variety
    if (/[a-z]/.test(this.newPassword)) strength++;
    if (/[A-Z]/.test(this.newPassword)) strength++;
    if (/[0-9]/.test(this.newPassword)) strength++;
    if (/[^a-zA-Z0-9]/.test(this.newPassword)) strength++;

    if (strength <= 2) {
      return { level: 33, text: 'Weak', color: 'warn' };
    } else if (strength <= 4) {
      return { level: 66, text: 'Medium', color: 'accent' };
    } else {
      return { level: 100, text: 'Strong', color: 'primary' };
    }
  }

  get isFormValid(): boolean {
    return !!(
      this.currentPassword &&
      this.newPassword &&
      this.confirmPassword &&
      this.newPassword === this.confirmPassword &&
      this.newPassword.length >= 8 &&
      /[a-z]/.test(this.newPassword) &&
      /[A-Z]/.test(this.newPassword) &&
      /[0-9]/.test(this.newPassword)
    );
  }

  async changePassword() {
    if (!this.isFormValid) {
      this.snackBar.open('Please fill all fields correctly', 'Close', { duration: 3000 });
      return;
    }

    if (this.newPassword !== this.confirmPassword) {
      this.snackBar.open('New passwords do not match', 'Close', { duration: 3000 });
      return;
    }

    this.saving = true;
    try {
      await this.apiService.changePassword(this.currentPassword, this.newPassword).toPromise();
      this.snackBar.open('Password changed successfully', 'Close', { duration: 3000 });
      
      // Clear form
      this.currentPassword = '';
      this.newPassword = '';
      this.confirmPassword = '';
    } catch (error: any) {
      const message = error.error?.detail || 'Failed to change password';
      this.snackBar.open(message, 'Close', { duration: 5000 });
    } finally {
      this.saving = false;
    }
  }
}