using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using MajlisManagement.DTOs;
using MajlisManagement.Services.Interfaces;
namespace MajlisManagement.Controllers;
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly IAuthService _auth;
public AuthController(IAuthService auth) => _auth = auth;
/// تسجيل مستخدم جديد
[HttpPost("register")]
[EnableRateLimiting("auth")]
public async Task Register([FromBody] RegisterDto dto)
{
var result = await _auth.RegisterAsync(dto);
return Ok(result);
}
/// تسجيل الدخول
[HttpPost("login")]
[EnableRateLimiting("auth")]
public async Task Login([FromBody] LoginDto dto)
{
var ip = HttpContext.Connection.RemoteIpAddress?.ToString();
var result = await _auth.LoginAsync(dto, ip);
return Ok(result);
}
/// تغيير كلمة المرور
[HttpPost("change-password")]
[Authorize]
public async Task ChangePassword([FromBody] ChangePasswordDto dto)
{
var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
await _auth.ChangePasswordAsync(userId, dto);
return Ok(new { message = "تم تغيير كلمة المرور بنجاح" });
}
/// بيانات المستخدم الحالي
[HttpGet("me")]
[Authorize]
public IActionResult Me()
{
return Ok(new
{
Id = User.FindFirstValue(ClaimTypes.NameIdentifier),
Name = User.FindFirstValue(ClaimTypes.Name),
Email = User.FindFirstValue(ClaimTypes.Email),
Role = User.FindFirstValue(ClaimTypes.Role)
});
}
}