Started implementing user settings -> security -> password management
This commit is contained in:
91
src/app/chat/user-settings/security/password/password.ts
Normal file
91
src/app/chat/user-settings/security/password/password.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import {Component, inject, signal} from '@angular/core';
|
||||
import {ServiceManager} from '../../../../service-manager';
|
||||
import {
|
||||
AbstractControl,
|
||||
FormControl,
|
||||
FormGroup,
|
||||
FormsModule,
|
||||
ReactiveFormsModule,
|
||||
ValidationErrors,
|
||||
Validators
|
||||
} from '@angular/forms';
|
||||
import {TranslatePipe} from '@ngx-translate/core';
|
||||
import {TuiBadge, TuiButtonLoading} from '@taiga-ui/kit';
|
||||
import {
|
||||
TuiButton,
|
||||
TuiDialog,
|
||||
TuiErrorComponent,
|
||||
TuiIcon,
|
||||
TuiInputDirective,
|
||||
TuiLabel,
|
||||
TuiTextfieldComponent
|
||||
} from '@taiga-ui/core';
|
||||
|
||||
@Component({
|
||||
selector: 'user-settings-security-password',
|
||||
imports: [
|
||||
FormsModule,
|
||||
ReactiveFormsModule,
|
||||
TranslatePipe,
|
||||
TuiBadge,
|
||||
TuiButton,
|
||||
TuiButtonLoading,
|
||||
TuiDialog,
|
||||
TuiErrorComponent,
|
||||
TuiIcon,
|
||||
TuiInputDirective,
|
||||
TuiLabel,
|
||||
TuiTextfieldComponent
|
||||
],
|
||||
templateUrl: './password.html',
|
||||
styleUrl: './password.scss',
|
||||
})
|
||||
export class Password {
|
||||
serviceManager = inject(ServiceManager)
|
||||
|
||||
changePasswordDialogOpen = signal(false)
|
||||
changePasswordRemoveMode = signal(false)
|
||||
changePasswordPending = signal(false)
|
||||
|
||||
changePasswordForm = new FormGroup({
|
||||
currentPassword: new FormControl(""),
|
||||
newPassword: new FormControl(""),
|
||||
newPasswordRepeat: new FormControl("")
|
||||
}, {
|
||||
validators: [(group) => this.chkPassMatch(group)]
|
||||
})
|
||||
|
||||
openChangePasswordDialog(modeRemove: boolean) {
|
||||
this.changePasswordDialogOpen.set(true)
|
||||
this.changePasswordRemoveMode.set(modeRemove)
|
||||
this.changePasswordForm.controls["currentPassword"].clearValidators()
|
||||
this.changePasswordForm.controls["newPassword"].clearValidators()
|
||||
this.changePasswordForm.controls["newPasswordRepeat"].clearValidators()
|
||||
if (!modeRemove) {
|
||||
this.changePasswordForm.controls["newPassword"].setValidators([Validators.required])
|
||||
this.changePasswordForm.controls["newPasswordRepeat"].setValidators([Validators.required])
|
||||
this.changePasswordForm.controls["newPassword"].updateValueAndValidity()
|
||||
this.changePasswordForm.controls["newPasswordRepeat"].updateValueAndValidity()
|
||||
}
|
||||
|
||||
this.changePasswordForm.controls["currentPassword"].setValidators([Validators.required])
|
||||
this.changePasswordForm.controls["currentPassword"].updateValueAndValidity()
|
||||
}
|
||||
|
||||
async changePassword(currentPassword: string | null, newPassword: string | null) {
|
||||
this.changePasswordPending.set(true)
|
||||
const service = this.serviceManager.currentSessionHandler
|
||||
if (service) {
|
||||
try {
|
||||
await service.changePassword(newPassword ?? "", currentPassword ?? "")
|
||||
} catch (e) {
|
||||
this.changePasswordForm.controls["currentPassword"].setErrors({incorrect: true})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
chkPassMatch(group: AbstractControl): ValidationErrors | null {
|
||||
return this.changePasswordRemoveMode() ? null : group.value.newPassword == group.value.newPasswordRepeat
|
||||
? null : {passMatchError: true};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user