193 lines
5.7 KiB
TypeScript
193 lines
5.7 KiB
TypeScript
import {
|
|
Component,
|
|
computed,
|
|
effect,
|
|
inject,
|
|
input,
|
|
signal,
|
|
} from '@angular/core';
|
|
import { AccountProfile, UserRole } from '../../api';
|
|
import { RolePipe } from '../../shared/pipes/role.pipe';
|
|
import { MatChipsModule } from '@angular/material/chips';
|
|
import { MatTabsModule } from '@angular/material/tabs';
|
|
import { MatDividerModule } from '@angular/material/divider';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
|
import {
|
|
form,
|
|
FormField,
|
|
maxLength,
|
|
minLength,
|
|
required,
|
|
validate,
|
|
FormRoot,
|
|
} from '@angular/forms/signals';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
|
|
import { CredentialService } from '../../shared/services/credential.service';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { Router } from '@angular/router';
|
|
|
|
interface ChangePasswordForm {
|
|
oldPassword: string;
|
|
newPassword: string;
|
|
newPasswordRepeat: string;
|
|
showPasswords: boolean;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-account',
|
|
imports: [
|
|
RolePipe,
|
|
MatChipsModule,
|
|
MatTabsModule,
|
|
MatDividerModule,
|
|
MatInputModule,
|
|
FormsModule,
|
|
ReactiveFormsModule,
|
|
MatFormFieldModule,
|
|
MatDividerModule,
|
|
MatIconModule,
|
|
MatButtonModule,
|
|
MatSlideToggleModule,
|
|
FormField,
|
|
FormRoot,
|
|
],
|
|
templateUrl: './account.component.html',
|
|
styleUrl: './account.component.scss',
|
|
})
|
|
export class AccountComponent {
|
|
readonly accountProfile = input.required<AccountProfile>();
|
|
|
|
private readonly _credentialService = inject(CredentialService);
|
|
private readonly _snackBar = inject(MatSnackBar);
|
|
private readonly _router = inject(Router);
|
|
|
|
roles: UserRole[] = [];
|
|
|
|
changePasswordModel = signal<ChangePasswordForm>({
|
|
oldPassword: '',
|
|
newPassword: '',
|
|
newPasswordRepeat: '',
|
|
showPasswords: false,
|
|
});
|
|
|
|
oldPasswordErrorMessage = computed<string | null>(() => {
|
|
const errors = this.changePasswordForm.oldPassword().errors();
|
|
return errors.length > 0 && errors[0].message ? errors[0].message : null;
|
|
});
|
|
|
|
newPasswordErrorMessage = computed<string | null>(() => {
|
|
const errors = this.changePasswordForm.newPassword().errors();
|
|
return errors.length > 0 && errors[0].message ? errors[0].message : null;
|
|
});
|
|
|
|
newPasswordRepeatErrorMessage = computed<string | null>(() => {
|
|
const errors = this.changePasswordForm.newPasswordRepeat().errors();
|
|
return errors.length > 0 && errors[0].message ? errors[0].message : null;
|
|
});
|
|
|
|
changePasswordForm = form(
|
|
this.changePasswordModel,
|
|
(schemaPath) => {
|
|
required(schemaPath.oldPassword, {
|
|
message: 'Altes Passwort ist erforderlich.',
|
|
});
|
|
required(schemaPath.newPassword, {
|
|
message: 'Neues Passwort ist erforderlich.',
|
|
});
|
|
required(schemaPath.newPasswordRepeat, {
|
|
message: 'Neues Passwort wiederholen ist erforderlich.',
|
|
});
|
|
|
|
minLength(schemaPath.newPassword, 8, {
|
|
message: 'Neues Passwort muss mindestens 8 Zeichen lang sein.',
|
|
});
|
|
maxLength(schemaPath.newPassword, 64, {
|
|
message: 'Neues Passwort darf maximal 64 Zeichen lang sein.',
|
|
});
|
|
|
|
validate(schemaPath.newPasswordRepeat, ({ value, valueOf }) => {
|
|
const newPassword = valueOf(schemaPath.newPassword).trim();
|
|
const newPasswordRepeat = value().trim();
|
|
|
|
if (newPassword !== newPasswordRepeat) {
|
|
return {
|
|
kind: 'passwordMismatch',
|
|
message: 'Passwörter stimmen nicht überein.',
|
|
};
|
|
}
|
|
|
|
return null;
|
|
});
|
|
|
|
validate(schemaPath.newPassword, ({ value }) => {
|
|
const oldPassword = this.changePasswordModel().oldPassword.trim();
|
|
const newPassword = value().trim();
|
|
|
|
if (oldPassword && newPassword && oldPassword === newPassword) {
|
|
return {
|
|
kind: 'passwordSameAsOld',
|
|
message:
|
|
'Neues Passwort darf nicht dasselbe wie das alte Passwort sein.',
|
|
};
|
|
}
|
|
|
|
return null;
|
|
});
|
|
},
|
|
{
|
|
submission: {
|
|
action: async (field) => {
|
|
const oldPassword = field.oldPassword().value().trim();
|
|
const newPassword = field.newPassword().value().trim();
|
|
|
|
if (!oldPassword || !newPassword) {
|
|
return {} as any;
|
|
}
|
|
this._credentialService
|
|
.changePassword(oldPassword, newPassword)
|
|
.subscribe({
|
|
next: () => {
|
|
this._credentialService.refreshAccessToken().subscribe({
|
|
next: () => {
|
|
this._router.navigate(['/dashboard']);
|
|
},
|
|
error: (error) => {
|
|
this._snackBar.open(
|
|
'Fehler beim Aktualisieren des Zugriffstokens. Bitte melden Sie sich erneut an.',
|
|
'OK',
|
|
{ duration: 7000 },
|
|
);
|
|
},
|
|
});
|
|
},
|
|
error: (error) => {
|
|
console.error('Error changing password:', error);
|
|
this._snackBar.open(
|
|
'Fehler beim Ändern des Passworts. Bitte versuchen Sie es erneut.',
|
|
'OK',
|
|
{ duration: 7000 },
|
|
);
|
|
},
|
|
});
|
|
|
|
return {} as any;
|
|
},
|
|
},
|
|
},
|
|
);
|
|
|
|
inputFieldType = computed(() =>
|
|
this.changePasswordModel().showPasswords ? 'text' : 'password',
|
|
);
|
|
|
|
constructor() {
|
|
effect(() => {
|
|
this.roles = this.accountProfile().roles;
|
|
});
|
|
}
|
|
}
|