Spaces:
Building
Building
File size: 5,678 Bytes
9f79da5 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
import { Component, inject, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
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';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { ApiService } from '../../services/api.service';
import { AuthService } from '../../services/auth.service';
import { Subject, takeUntil } from 'rxjs';
@Component({
selector: 'app-user-info',
standalone: true,
imports: [
CommonModule,
FormsModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatIconModule,
MatProgressBarModule,
MatCardModule,
MatSnackBarModule,
MatProgressSpinnerModule
],
templateUrl: './user-info.component.html',
styleUrls: ['./user-info.component.scss']
})
export class UserInfoComponent implements OnDestroy {
private apiService = inject(ApiService);
private authService = inject(AuthService);
private snackBar = inject(MatSnackBar);
private router = inject(Router);
username = this.authService.getUsername() || '';
currentPassword = '';
newPassword = '';
confirmPassword = '';
saving = false;
showCurrentPassword = false;
showNewPassword = false;
showConfirmPassword = false;
// Memory leak prevention
private destroyed$ = new Subject<void>();
ngOnDestroy() {
this.destroyed$.next();
this.destroyed$.complete();
}
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.newPassword === this.confirmPassword &&
this.newPassword.length >= 8;
}
changePassword() {
if (!this.isFormValid || this.saving) return;
this.saving = true;
this.apiService.changePassword(this.currentPassword, this.newPassword)
.pipe(takeUntil(this.destroyed$))
.subscribe({
next: () => {
this.saving = false;
// Clear form
this.currentPassword = '';
this.newPassword = '';
this.confirmPassword = '';
// Show success message
this.snackBar.open(
'Password changed successfully. Please login again.',
'OK',
{
duration: 5000,
panelClass: ['success-snackbar']
}
).afterDismissed().subscribe(() => {
// Logout after password change
this.authService.logout();
});
},
error: (error) => {
this.saving = false;
// Handle validation errors
if (error.status === 422 && error.error?.details) {
// Field-level errors
const fieldErrors = error.error.details;
if (fieldErrors.some((e: any) => e.field === 'current_password')) {
this.snackBar.open(
'Current password is incorrect',
'Close',
{
duration: 5000,
panelClass: ['error-snackbar']
}
);
} else if (fieldErrors.some((e: any) => e.field === 'new_password')) {
const pwError = fieldErrors.find((e: any) => e.field === 'new_password');
this.snackBar.open(
pwError.message || 'New password does not meet requirements',
'Close',
{
duration: 5000,
panelClass: ['error-snackbar']
}
);
}
} else {
// Generic error
this.snackBar.open(
error.error?.detail || 'Failed to change password',
'Close',
{
duration: 5000,
panelClass: ['error-snackbar']
}
);
}
}
});
}
togglePasswordVisibility(field: 'current' | 'new' | 'confirm') {
switch(field) {
case 'current':
this.showCurrentPassword = !this.showCurrentPassword;
break;
case 'new':
this.showNewPassword = !this.showNewPassword;
break;
case 'confirm':
this.showConfirmPassword = !this.showConfirmPassword;
break;
}
}
} |