diff --git a/services/iam-service-net/src/IamService.API/Controllers/AuthController.cs b/services/iam-service-net/src/IamService.API/Controllers/AuthController.cs index 40526306..911d2197 100644 --- a/services/iam-service-net/src/IamService.API/Controllers/AuthController.cs +++ b/services/iam-service-net/src/IamService.API/Controllers/AuthController.cs @@ -77,74 +77,6 @@ public class AuthController : ControllerBase return CreatedAtAction(nameof(Register), new { id = result.UserId }, ApiResponse.Ok(result)); } - /// - /// EN: Login with email and password (Resource Owner Password Grant). - /// VI: Đăng nhập với email và password (Resource Owner Password Grant). - /// - /// Login credentials - /// Cancellation token - /// Login result with token info - [HttpPost("login")] - [SwaggerOperation( - Summary = "Login with credentials", - Description = "Authenticates a user with email and password. For full OAuth2 flow, use /connect/token endpoint.", - OperationId = "Login")] - [SwaggerResponse(StatusCodes.Status200OK, "Login successful")] - [SwaggerResponse(StatusCodes.Status400BadRequest, "Invalid credentials")] - [SwaggerResponse(StatusCodes.Status403Forbidden, "Account locked")] - [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task Login( - [FromBody, SwaggerRequestBody("Login credentials", Required = true)] LoginRequest request, - CancellationToken cancellationToken) - { - var user = await _userManager.FindByEmailAsync(request.Email); - if (user == null) - { - _logger.LogWarning("Login failed: user not found for {Email}", request.Email); - return BadRequest(ApiResponse.Fail("INVALID_CREDENTIALS", "Invalid email or password.")); - } - - // EN: Check password - // VI: Kiểm tra password - var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, lockoutOnFailure: true); - - if (result.IsLockedOut) - { - _logger.LogWarning("Login failed: user {UserId} is locked out", user.Id); - await _events.RaiseAsync(new UserLoginFailureEvent(user.Email!, "User locked out", clientId: null)); - return StatusCode(StatusCodes.Status403Forbidden, - ApiResponse.Fail("ACCOUNT_LOCKED", "Account is locked. Please try again later.")); - } - - if (!result.Succeeded) - { - _logger.LogWarning("Login failed: invalid password for user {UserId}", user.Id); - await _events.RaiseAsync(new UserLoginFailureEvent(user.Email!, "Invalid credentials", clientId: null)); - return BadRequest(ApiResponse.Fail("INVALID_CREDENTIALS", "Invalid email or password.")); - } - - // EN: Record login - // VI: Ghi nhận login - user.RecordLogin(); - await _userManager.UpdateAsync(user); - - await _events.RaiseAsync(new UserLoginSuccessEvent(user.Email!, user.Id.ToString(), user.FullName, clientId: null)); - - _logger.LogInformation("User {UserId} logged in successfully", user.Id); - - // EN: Note: Full token response requires OAuth2 flow via /connect/token - // VI: Lưu ý: Response token đầy đủ yêu cầu OAuth2 flow qua /connect/token - return Ok(ApiResponse.Ok(new LoginResponse - { - Success = true, - Message = "Login successful. Use /connect/token with grant_type=password for access tokens.", - UserId = user.Id, - Email = user.Email!, - FullName = user.FullName - })); - } /// /// EN: Change user password. @@ -547,64 +479,6 @@ public class AuthController : ControllerBase #region Request/Response Models -/// -/// EN: Login request body. -/// VI: Request body đăng nhập. -/// -public class LoginRequest -{ - /// - /// EN: User email. - /// VI: Email người dùng. - /// - /// user@example.com - public string Email { get; set; } = string.Empty; - - /// - /// EN: User password. - /// VI: Mật khẩu người dùng. - /// - /// Password123! - public string Password { get; set; } = string.Empty; -} - -/// -/// EN: Login response. -/// VI: Response đăng nhập. -/// -public class LoginResponse -{ - /// - /// EN: Whether the login was successful. - /// VI: Đăng nhập có thành công không. - /// - public bool Success { get; set; } - - /// - /// EN: Result message. - /// VI: Thông điệp kết quả. - /// - public string Message { get; set; } = string.Empty; - - /// - /// EN: User ID. - /// VI: ID người dùng. - /// - public Guid UserId { get; set; } - - /// - /// EN: User email. - /// VI: Email người dùng. - /// - public string Email { get; set; } = string.Empty; - - /// - /// EN: User full name. - /// VI: Tên đầy đủ người dùng. - /// - public string FullName { get; set; } = string.Empty; -} - /// /// EN: Request body for changing password. /// VI: Request body để đổi mật khẩu.