diff --git a/backend/src/StamAcasa.IdentityServer/Quickstart/Account/DeleteAccountController.cs b/backend/src/StamAcasa.IdentityServer/Quickstart/Account/DeleteAccountController.cs new file mode 100644 index 00000000..38e48dab --- /dev/null +++ b/backend/src/StamAcasa.IdentityServer/Quickstart/Account/DeleteAccountController.cs @@ -0,0 +1,48 @@ +using System; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using IdentityServer.Data; +using IdentityServer4; +using IdentityServer4.Models; +using IdentityServer4.Services; +using IdentityServer4.Validation; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; + +namespace StamAcasa.IdentityServer.Quickstart.Account +{ + [Route("api/[controller]")] + [ApiController] + [AllowAnonymous] + public class DeleteAccountController : ControllerBase + { + private readonly UserManager _userManager; + + public DeleteAccountController(UserManager userManager) + { + _userManager = userManager; + } + + [HttpPost] + public async Task DeleteAccountAsync([FromBody] DeleteAccountModel model) + { + var user = await _userManager.FindByNameAsync(model.Username); + if (user == null || !await _userManager.CheckPasswordAsync(user, model.Password)) + { + return new UnauthorizedResult(); + } + + var response = await _userManager.DeleteAsync(user); + if (!response.Succeeded) + { + return StatusCode(StatusCodes.Status500InternalServerError, $"Unexpected error occurred deleting user with ID '{user.Id}'."); + } + + return Ok(); + } + } +} \ No newline at end of file diff --git a/backend/src/StamAcasa.IdentityServer/Quickstart/Account/DeleteAccountModel.cs b/backend/src/StamAcasa.IdentityServer/Quickstart/Account/DeleteAccountModel.cs new file mode 100644 index 00000000..f7febb7e --- /dev/null +++ b/backend/src/StamAcasa.IdentityServer/Quickstart/Account/DeleteAccountModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace StamAcasa.IdentityServer.Quickstart.Account +{ + public class DeleteAccountModel + { + public string Username { get; set; } + + public string Password { get; set; } + } +}