using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; using SharpbinV3.Server.Data.Entities; using SharpbinV3.Server.DTOs; using SharpbinV3.Server.Services; using Bcrypt = BCrypt.Net.BCrypt; namespace SharpbinV3.Server.Controllers { [ApiController] [Route("api/[controller]")] public class AuthController(IUserService userService, IAuthService authService) : ControllerBase { private readonly IAuthService _authService = authService; private readonly IUserService _userService = userService; [HttpPost] [EnableRateLimiting("Sliding")] [Route("register")] public async Task Register([FromBody] RegisterRequest request) { var existingUser = await _userService.GetByUsername(request.Username); if (existingUser != null) return Conflict(new { message = "Username already exists." }); if (!string.IsNullOrEmpty(request.Email)) { var existingEmailUser = await _userService.GetByEmail(request.Email); if (existingEmailUser != null) return Conflict(new { message = "Email already in use." }); if (!IsValidEmail(request.Email)) return BadRequest(new { message = "Invalid email." }); } if (!IsValidPassword(request.Password)) return BadRequest(new { message = "Password should have at least 8 characters, including uppercase, lowercase, and digits." }); if (!string.IsNullOrEmpty(request.DisplayName) && request.DisplayName.Length > 26) return BadRequest(new { message = "Display name should not exceed 26 characters." }); var user = await _authService.CreateUser(request.Username, request.Password, request.Email, request.DisplayName); return Ok(new { message = "User registered successfully.", user = new { user.UID, user.UUID, user.Username, user.Email, user.DisplayName, user.Roles } } ); } [HttpPost] [Route("login")] [EnableRateLimiting("Sliding")] public async Task Login([FromBody] LoginRequest request) { if (string.IsNullOrEmpty(request.Username) && string.IsNullOrEmpty(request.Email)) return BadRequest(new { message = "Username or email is required." }); User? user = null; if (!string.IsNullOrEmpty(request.Username)) user = await _userService.GetByUsername(request.Username); if (user == null && !string.IsNullOrEmpty(request.Email)) user = await _userService.GetByEmail(request.Email); if (user == null) return BadRequest(new { message = "Invalid username/email or password." }); if (!Bcrypt.Verify(request.Password, user.PasswordHash)) return BadRequest(new { message = "Invalid username/email or password." }); var token = _authService.GenerateJWTToken(user); await _authService.UpdateLoginTime(user); return Ok(new { token.Result }); } [HttpPost] [Route("refresh")] [EnableRateLimiting("Sliding")] public async Task RefreshToken([FromBody] RefreshTokenRequest request) { try { var jwtResult = await _authService.RefreshJWTToken(request.Token, request.RefreshToken); return Ok(new { token = jwtResult }); } catch (Exception ex) { return BadRequest(new { message = ex.Message }); } } private static bool IsValidPassword(string password) { if (password.Length < 6) return false; if (!password.Any(char.IsUpper)) return false; if (!password.Any(char.IsLower)) return false; if (!password.Any(char.IsDigit)) return false; return true; } private static bool IsValidEmail(string email) { try { var addr = new System.Net.Mail.MailAddress(email); return addr.Address == email; } catch { return false; } } } }