-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from TunNetCom/add-change-password
Replace controller with minimal API and add change password
- Loading branch information
Showing
11 changed files
with
178 additions
and
60 deletions.
There are no files selected for viewing
42 changes: 0 additions & 42 deletions
42
src/TimeLogIdentityService/IdentityService.API/Controllers/IdentityController.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/TimeLogIdentityService/IdentityService.API/IdentityEndPoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
namespace IdentityService.API; | ||
|
||
public static class IdentityEndPoint | ||
{ | ||
public static void AddEndpoints(this IEndpointRouteBuilder app) | ||
{ | ||
_ = app.MapPost("/CreateAccount", async (IMediator _mediator, [FromBody] CreateAccountCommand request) => | ||
{ | ||
IdentityResult userResponse = await _mediator.Send(request); | ||
if (!userResponse.Succeeded) | ||
{ | ||
return Results.BadRequest(userResponse); | ||
} | ||
return Results.Ok(userResponse); | ||
}); | ||
|
||
_ = app.MapPost("/Login", async (IMediator _mediator, [FromBody] LoginCommand request) => | ||
{ | ||
ApiResponse<LoginResponse> userResponse = await _mediator.Send(request); | ||
if (!userResponse.Succeeded) | ||
{ | ||
return Results.BadRequest(userResponse); | ||
} | ||
return Results.Ok(userResponse); | ||
}); | ||
|
||
_ = app.MapPost("/AddRole", async (IMediator _mediator, [FromBody] AddRoleCommand request) => | ||
{ | ||
IdentityResult roleResponse = await _mediator.Send(request); | ||
if (!roleResponse.Succeeded) | ||
{ | ||
return Results.BadRequest(roleResponse); | ||
} | ||
return Results.Ok(roleResponse); | ||
}); | ||
|
||
_ = app.MapPost("/AttachUserToRole", async (IMediator _mediator, [FromBody] AttachUserToRoleCommand request) => | ||
{ | ||
ApiResponse<UserToRoleResponse> response = await _mediator.Send(request); | ||
if (!response.Succeeded) | ||
{ | ||
return Results.BadRequest(response); | ||
} | ||
return Results.Ok(response); | ||
}); | ||
|
||
_ = app.MapPost("/ChangePassword", async (IMediator _mediator, [FromBody] ChangePasswordCommand request) => | ||
{ | ||
ApiResponse response = await _mediator.Send(request); | ||
if (!response.Succeeded) | ||
{ | ||
return Results.BadRequest(response); | ||
} | ||
return Results.Ok(response); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
_ = app.UseAuthorization(); | ||
|
||
_ = app.MapControllers(); | ||
app.AddEndpoints(); | ||
|
||
app.Run(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...ntityService/IdentityService.Application/Features/ChangePassword/ChangePasswordCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
namespace IdentityService.Application.Features.ChangePassword | ||
{ | ||
public record class ChangePasswordCommand(string OldPassword, string NewPassword, string Email) : IRequest<ApiResponse>; | ||
} |
50 changes: 50 additions & 0 deletions
50
...rvice/IdentityService.Application/Features/ChangePassword/ChangePasswordCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using IdentityService.Contracts.Constant; | ||
|
||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
namespace IdentityService.Application.Features.ChangePassword | ||
{ | ||
public class ChangePasswordCommandHandler(UserManager<IdentityUser> userManager) : | ||
IRequestHandler<ChangePasswordCommand, ApiResponse> | ||
{ | ||
private readonly UserManager<IdentityUser> _userManager = userManager; | ||
|
||
public async Task<ApiResponse> Handle(ChangePasswordCommand request, CancellationToken cancellationToken) | ||
{ | ||
IdentityUser? user = await _userManager.FindByEmailAsync(request.Email); | ||
if (user == null) | ||
{ | ||
return new ApiResponse() | ||
{ | ||
Succeeded = false, | ||
Error = new ProblemDetails() | ||
{ | ||
Title = nameof(ErrorDetails.InvalidEmail), | ||
Detail = ErrorDetails.InvalidEmail, | ||
Status = 404, | ||
}, | ||
}; | ||
} | ||
|
||
if (!await _userManager.CheckPasswordAsync(user, request.OldPassword)) | ||
{ | ||
return new ApiResponse() | ||
{ | ||
Succeeded = false, | ||
Error = new ProblemDetails() | ||
{ | ||
Title = nameof(ErrorDetails.InvalidOldPassword), | ||
Detail = ErrorDetails.InvalidOldPassword, | ||
Status = 400, | ||
}, | ||
}; | ||
} | ||
|
||
_ = await _userManager.ChangePasswordAsync(user, request.OldPassword, request.NewPassword); | ||
return new ApiResponse() | ||
{ | ||
Succeeded = false, | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/TimeLogIdentityService/IdentityService.Contracts/BaseResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
namespace IdentityService.Contracts | ||
{ | ||
public class ApiResponse(object? data = null) | ||
{ | ||
public bool Succeeded { get; set; } | ||
|
||
public object? Data { get; set; } = data; | ||
|
||
public ProblemDetails? Error { get; set; } = new(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/TimeLogIdentityService/IdentityService.Contracts/Constant/ErrorDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Reflection; | ||
|
||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
namespace IdentityService.Contracts.Constant | ||
{ | ||
public static class ErrorDetails | ||
{ | ||
public const string InvalidUserName = "Invalid UserName"; | ||
|
||
public const string InvalidPassword = "Invalid Password"; | ||
|
||
public const string InvalidEmail = "Invalid Email"; | ||
|
||
public const string InvalidUserId = "Invalid UserId"; | ||
|
||
public const string InvalidOldPassword = "Invalid OldPassword"; | ||
|
||
public const string UserNotFound = "User Not Found"; | ||
|
||
public const string RoleNotFound = "Role Not Found"; | ||
|
||
public const string RoleAssignmentFailed = "Role Assignment Failed"; | ||
} | ||
} |