using FindAndDrive.Domain.Interfaces.Services;
using FindAndDrive.Domain.Models.DTO.Request;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FindAndDrive.API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class AuthenticationController : ControllerBase
{
private readonly IAuthenticationService _authenticationService;
public AuthenticationController(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
///
/// Generate a bearer token
///
[AllowAnonymous]
[HttpPost("token")]
public async Task GetToken([FromBody] TokenRequest request)
{
var response = await _authenticationService.Authenticate(request.ApiKeyId, request.ApiSecret);
if (!response.Success) return Unauthorized(response.Message);
return Ok(response.ResponseObject);
}
///
/// Refresh your current token
///
[Authorize]
[HttpPost("refresh")]
public async Task RefreshToken()
{
var token = Request.Headers["Authorization"]
.First()
.Split(" ")
.Last();
var response = await _authenticationService.RefreshToken(token);
if (!response.Success) return Unauthorized(response.Message);
return Ok(response.ResponseObject);
}
}