Skip to content
Snippets Groups Projects
Commit 00ace4d4 authored by Radek Puš's avatar Radek Puš
Browse files

remove account action

parent 2aefdcf0
No related branches found
No related tags found
No related merge requests found
<div> <div>
<button (click)="logout()">Odhlásit se</button> <button (click)="logout()">Odhlásit se</button>
<button mat-raised-button (click)="openDialog()">Změnit heslo</button> <button mat-raised-button (click)="openChangePasswordDialog()">Změnit heslo</button>
<button mat-raised-button (click)="openDeleteUserDialog()">Zrušit účet</button>
</div> </div>
...@@ -2,6 +2,7 @@ import { Component } from '@angular/core'; ...@@ -2,6 +2,7 @@ import { Component } from '@angular/core';
import { LoginService } from '../../../login/login.service'; import { LoginService } from '../../../login/login.service';
import { ChangePasswordComponent } from './change-password/change-password.component'; import { ChangePasswordComponent } from './change-password/change-password.component';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { DeleteUserComponent } from './delete-user/delete-user.component';
   
@Component({ @Component({
selector: 'app-account-properties', selector: 'app-account-properties',
...@@ -19,9 +20,21 @@ export class AccountPropertiesComponent { ...@@ -19,9 +20,21 @@ export class AccountPropertiesComponent {
} }
   
animal: string; animal: string;
name: string; name: string;
   
openDialog(): void { openDeleteUserDialog(): void {
const dialogRef = this.dialog.open(DeleteUserComponent, {
width: '250px',
data: { name: this.name, animal: this.animal }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.animal = result;
});
}
openChangePasswordDialog(): void {
const dialogRef = this.dialog.open(ChangePasswordComponent, { const dialogRef = this.dialog.open(ChangePasswordComponent, {
width: '250px', width: '250px',
data: { name: this.name, animal: this.animal } data: { name: this.name, animal: this.animal }
......
...@@ -8,32 +8,35 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; ...@@ -8,32 +8,35 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { JwtInterceptor } from "../../../helpers/jwt.interceptor"; import { JwtInterceptor } from "../../../helpers/jwt.interceptor";
import { AccountPropertiesComponent } from "./account-properties.component"; import { AccountPropertiesComponent } from "./account-properties.component";
import { ChangePasswordComponent } from "./change-password/change-password.component"; import { ChangePasswordComponent } from "./change-password/change-password.component";
import { DeleteUserComponent } from "./delete-user/delete-user.component";
   
   
   
   
@NgModule({ @NgModule({
declarations: [ declarations: [
AccountPropertiesComponent, AccountPropertiesComponent,
ChangePasswordComponent ChangePasswordComponent,
], DeleteUserComponent
imports: [ ],
CommonModule, imports: [
FormsModule, CommonModule,
ReactiveFormsModule, FormsModule,
MatDialogModule, ReactiveFormsModule,
MatFormFieldModule, MatDialogModule,
MatInputModule, MatFormFieldModule,
BrowserAnimationsModule MatInputModule,
], BrowserAnimationsModule
exports: [ ],
AccountPropertiesComponent exports: [
], AccountPropertiesComponent
entryComponents: [ ],
ChangePasswordComponent entryComponents: [
], ChangePasswordComponent,
providers: [ DeleteUserComponent
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true } ],
] providers: [
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true }
]
}) })
export class AccountPropertiesModule { } export class AccountPropertiesModule { }
<h1 mat-dialog-title>Smazat účet</h1>
<form [formGroup]="InsertForm" (submit)="onChangeClick($event)">
<div mat-dialog-content>
<div>
<p>Opravdu chcete smazat účet? Tato akce je nevratná!</p>
<p>Pro ověření zadejte prosím znovu heslo:</p>
</div>
<div>
<input formControlName="Password" type="password" placeholder="heslo" id="password" />
<div *ngIf="Password.touched && Password.errors" class="errorInput"><span *ngIf="Password.hasError('required')">Vyplňte heslo</span></div>
</div>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()" cdkFocusInitial [mat-dialog-close]> Storno </button>
<input type="submit" value="Odeslat" [disabled]="InsertForm.invalid" />
</div>
</form>
import { Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { FormControl, FormBuilder, Validators, FormGroup } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-delete-user',
templateUrl: './delete-user.component.html',
styleUrls: ['./delete-user.component.css']
})
/** ChangePassword component*/
export class DeleteUserComponent implements OnInit {
Password: FormControl;
InsertForm: FormGroup;
IsChangeValid: boolean = false;
ErrorMessage: string = "";
/** ChangePassword ctor */
constructor(
public dialogRef: MatDialogRef<DeleteUserComponent>,
@Inject(MAT_DIALOG_DATA) public data: DialogData,
private FormBuilder: FormBuilder,
private HttpClient: HttpClient
) { }
ngOnInit() {
this.Password = new FormControl('', [Validators.required]);
this.InsertForm = this.FormBuilder.group({ "Password": this.Password });
}
onNoClick(): void {
this.dialogRef.close();
}
onChangeClick(event): void {
this.deleteUser(event);
}
deleteUser(event): void {
event.preventDefault();
const target = event.target;
const password = target.querySelector('#password').value;
this.HttpClient.delete<{ status: number, messasge: string }>('api/Registration', password).subscribe((res: any) => {
console.log('Deletion of user successful: ', res.status.toString());
this.IsChangeValid = false;
if (res.status = 1) {
this.IsChangeValid = true;
this.dialogRef.close();
}
}, error => {
console.error("Deletion of user error:", error);
this.IsChangeValid = false;
this.ErrorMessage = "Nesprávné heslo";
});
}
}
export interface DialogData {
animal: string;
name: string;
}
...@@ -58,5 +58,27 @@ namespace Core.Controllers ...@@ -58,5 +58,27 @@ namespace Core.Controllers
   
return Ok(new { status = 1, message = "Password change was successful" }); return Ok(new { status = 1, message = "Password change was successful" });
} }
// Delete: api/Registration
[HttpDelete]
[Authorize(Policy = "RequireLoggedIn")]
public IActionResult Delete([FromBody] string password)
{
Claim userJwtID = User.Claims.First(c => c.Type == "UserID");
long userID = long.Parse(userJwtID.Value);
Models.User user = Context.Users.FirstOrDefault(u => u.ID == userID);
if (user == null)
return Unauthorized(new { message = "user not found" });
if (!PasswordHasher.Check(user.Password, password))
return Unauthorized(new { message = "wrong password" });
Context.Remove(user);
Context.SaveChanges();
return Ok(new { status = 1, message = "Password change was successful" });
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment