44using Microsoft . AspNetCore . Authorization ;
55using WebApi . Entities ;
66using MediatR ;
7+ using System . Security . Claims ;
8+ using Microsoft . AspNetCore . Http ;
79
810namespace WebApi . Features . Accounts
911{
@@ -12,10 +14,12 @@ namespace WebApi.Features.Accounts
1214 public class AccountsController : ControllerBase
1315 {
1416 private readonly IMediator _mediator ;
17+ private readonly IHttpContextAccessor _httpContext ;
1518
16- public AccountsController ( IMediator mediator )
19+ public AccountsController ( IMediator mediator , IHttpContextAccessor httpContext )
1720 {
1821 _mediator = mediator ;
22+ _httpContext = httpContext ;
1923 }
2024
2125 // POST: api/accounts/register
@@ -25,11 +29,13 @@ public async Task<IActionResult> Register(RegisterViewModel viewModel)
2529 {
2630 // mediator from Features/Employees
2731 var isCardExist = await _mediator . Send ( new Employees . IsCardExists . Query ( Guid . Empty , viewModel . CardNo ) ) ;
28- if ( isCardExist ) return BadRequest ( "Card No. is already in use" ) ;
32+ if ( isCardExist )
33+ return BadRequest ( "Card No. is already in use" ) ;
2934
3035 // mediator from Features/Employees
3136 var isUsernameExist = await _mediator . Send ( new Auth . IsUserExists . Query ( viewModel . UserName ) ) ;
32- if ( isUsernameExist ) return BadRequest ( $ "Username { viewModel . UserName } is already taken") ;
37+ if ( isUsernameExist )
38+ return BadRequest ( $ "Username { viewModel . UserName } is already taken") ;
3339
3440 // Create user account
3541 var employeeInfo = await _mediator . Send ( new Register . Command ( viewModel ) ) ;
@@ -40,11 +46,12 @@ public async Task<IActionResult> Register(RegisterViewModel viewModel)
4046 // PUT: api/accounts/update-password
4147 [ Authorize ( Roles = "Admin" ) ]
4248 [ HttpPut ( "update-password" ) ]
43- public async Task < IActionResult > UpdatePassword ( ChangePasswordViewModel viewModel )
49+ public async Task < IActionResult > UpdatePassword ( UpdatePasswordViewModel viewModel )
4450 {
4551 // Change a specific Employee account's password
4652 var result = await _mediator . Send ( new UpdatePassword . Command ( viewModel ) ) ;
47- if ( ! result ) return StatusCode ( 500 ) ;
53+ if ( ! result )
54+ return BadRequest ( ) ;
4855
4956 return Ok ( ) ;
5057 }
@@ -54,12 +61,15 @@ public async Task<IActionResult> UpdatePassword(ChangePasswordViewModel viewMode
5461 public async Task < IActionResult > ChangePassword ( ChangePasswordViewModel viewModel )
5562 {
5663 // Check if Old password is correct
57- var validatePassword = await _mediator . Send ( new Auth . ValidatePassword . Query ( viewModel . UserName , viewModel . OldPassword ) ) ;
58- if ( ! validatePassword ) return BadRequest ( "Incorrect password" ) ;
64+ var currentUser = _httpContext . HttpContext . User . FindFirstValue ( ClaimTypes . NameIdentifier ) ;
65+ var validatePassword = await _mediator . Send ( new Auth . ValidatePassword . Query ( currentUser , viewModel . OldPassword ) ) ;
66+ if ( ! validatePassword )
67+ return BadRequest ( "Incorrect password" ) ;
5968
6069 // Change account password
6170 var result = await _mediator . Send ( new ChangePassword . Command ( viewModel ) ) ;
62- if ( ! result . Succeeded ) return BadRequest ( "Unable to change password" ) ;
71+ if ( ! result . Succeeded )
72+ return BadRequest ( "Unable to change password" ) ;
6373
6474 return Ok ( ) ;
6575 }
0 commit comments