using Managing.Api.Authorization;
using Managing.Api.Models.Requests;
using Managing.Application.Abstractions.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Managing.Api.Controllers;
///
/// Provides authentication-related actions, including token creation for user authentication.
///
[ApiController]
[Route("[controller]")]
[Produces("application/json")]
public class UserController : ControllerBase
{
private IConfiguration _config;
private readonly IUserService _userService;
private readonly IJwtUtils _jwtUtils;
///
/// Initializes a new instance of the class.
///
/// Configuration settings.
/// Service for user-related operations.
/// Utility for JWT token operations.
public UserController(IConfiguration config, IUserService userService, IJwtUtils jwtUtils)
{
_config = config;
_userService = userService;
_jwtUtils = jwtUtils;
}
///
/// Creates a JWT token for a user based on the provided login credentials.
///
/// The login request containing user credentials.
/// A JWT token if authentication is successful; otherwise, an Unauthorized result.
[AllowAnonymous]
[HttpPost]
public async Task> CreateToken([FromBody] LoginRequest login)
{
var user = await _userService.Authenticate(login.Name, login.Address, login.Message, login.Signature);
if (user != null)
{
var tokenString = _jwtUtils.GenerateJwtToken(user, login.Address);
return Ok(tokenString);
}
return Unauthorized();
}
}