Feat: Cambios Varios 2
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SIGCM.Application.DTOs;
|
||||
using SIGCM.Application.Interfaces;
|
||||
@@ -15,12 +16,60 @@ public class AuthController : ControllerBase
|
||||
_authService = authService;
|
||||
}
|
||||
|
||||
// Inicio de sesión tradicional
|
||||
[HttpPost("login")]
|
||||
public async Task<IActionResult> Login(LoginDto dto)
|
||||
{
|
||||
var token = await _authService.LoginAsync(dto.Username, dto.Password);
|
||||
if (token == null) return Unauthorized("Invalid credentials");
|
||||
var result = await _authService.LoginAsync(dto.Username, dto.Password);
|
||||
if (!result.Success) return Unauthorized(new { message = result.ErrorMessage });
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// Registro de nuevos usuarios
|
||||
[HttpPost("register")]
|
||||
public async Task<IActionResult> Register(RegisterDto dto)
|
||||
{
|
||||
var result = await _authService.RegisterAsync(dto.Username, dto.Email, dto.Password);
|
||||
if (!result.Success) return BadRequest(new { message = result.ErrorMessage });
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// Inicio de sesión con Google
|
||||
[HttpPost("google-login")]
|
||||
public async Task<IActionResult> GoogleLogin([FromBody] string idToken)
|
||||
{
|
||||
var result = await _authService.GoogleLoginAsync(idToken);
|
||||
if (!result.Success) return Unauthorized(new { message = result.ErrorMessage });
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
// Flujo MFA: Obtener secreto (QR)
|
||||
[Authorize]
|
||||
[HttpGet("mfa/setup")]
|
||||
public async Task<IActionResult> SetupMfa()
|
||||
{
|
||||
var userId = int.Parse(User.FindFirst("Id")?.Value!);
|
||||
var secret = await _authService.GenerateMfaSecretAsync(userId);
|
||||
return Ok(new { secret, qrCodeUri = $"otpauth://totp/SIGCM:{User.Identity?.Name}?secret={secret}&issuer=SIGCM" });
|
||||
}
|
||||
|
||||
// Flujo MFA: Verificar y activar
|
||||
[Authorize]
|
||||
[HttpPost("mfa/verify")]
|
||||
public async Task<IActionResult> VerifyMfa([FromBody] string code)
|
||||
{
|
||||
var userId = int.Parse(User.FindFirst("Id")?.Value!);
|
||||
var valid = await _authService.VerifyMfaCodeAsync(userId, code);
|
||||
if (!valid) return BadRequest(new { message = "Código inválido" });
|
||||
|
||||
return Ok(new { token });
|
||||
await _authService.EnableMfaAsync(userId, true);
|
||||
return Ok(new { success = true });
|
||||
}
|
||||
}
|
||||
|
||||
public class RegisterDto
|
||||
{
|
||||
public string Username { get; set; } = "";
|
||||
public string Email { get; set; } = "";
|
||||
public string Password { get; set; } = "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user