Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

95 as a user i would like deleting my account to require password confirmation #97

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions GirafAPI/Endpoints/UserEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GirafAPI.Mapping;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace GirafAPI.Endpoints;
Expand Down Expand Up @@ -129,16 +130,31 @@ public static RouteGroupBuilder MapUsersEndpoints(this WebApplication app)
.Produces(StatusCodes.Status200OK)
.Produces<IEnumerable<IdentityError>>(StatusCodes.Status400BadRequest);

group.MapDelete("/{id}", async (string id, UserManager<GirafUser> userManager) =>
//[FromBody] is needed by ASP NETs .MapDelete method
group.MapDelete("/{id}", async ([FromBody] DeleteUserDTO deleteUserDTO, UserManager<GirafUser> userManager) =>
{
var user = await userManager.FindByIdAsync(id);
try {
var user = await userManager.FindByIdAsync(deleteUserDTO.Id);

if(user == null) {
return Results.BadRequest("Invalid user id.");
}
if(user == null) {
return Results.BadRequest("Invalid user id.");
}

var passwordValid = await userManager.CheckPasswordAsync(user, deleteUserDTO.Password);

var result = await userManager.DeleteAsync(user);
return result.Succeeded ? Results.NoContent() : Results.BadRequest(result.Errors);
if(!passwordValid) {
return Results.BadRequest("Invalid password");
}

await userManager.DeleteAsync(user);
return Results.NoContent();
}
catch (Exception)
{
//unexpected error
return Results.Problem("An error occurred while trying to delete user.", statusCode: StatusCodes.Status500InternalServerError);
}

})
.WithName("DeleteUser")
.WithTags("Users")
Expand Down
11 changes: 11 additions & 0 deletions GirafAPI/Entities/Users/DTOs/DeleteUserDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace GirafAPI.Entities.Users.DTOs;

public record DeleteUserDTO
{
[Required] public required string Id { get; set; }

[Required] public required string Password { get; set; }

}