75 lines
2.0 KiB
C#
75 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using GreenHome.Application;
|
|
|
|
namespace GreenHome.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService authService;
|
|
|
|
public AuthController(IAuthService authService)
|
|
{
|
|
this.authService = authService;
|
|
}
|
|
|
|
[HttpPost("send-code")]
|
|
public async Task<ActionResult<SendCodeResponse>> SendCode(
|
|
[FromBody] SendCodeRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await authService.SendVerificationCodeAsync(request, cancellationToken);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("verify-code")]
|
|
public async Task<ActionResult<VerifyCodeResponse>> VerifyCode(
|
|
[FromBody] VerifyCodeRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await authService.VerifyCodeAsync(request, cancellationToken);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("can-resend")]
|
|
public async Task<ActionResult<bool>> CanResend(
|
|
[FromQuery] string mobile,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var canResend = await authService.CanResendCodeAsync(mobile, cancellationToken);
|
|
return Ok(new { canResend });
|
|
}
|
|
|
|
[HttpGet("me")]
|
|
public async Task<ActionResult<UserDto>> Me(CancellationToken cancellationToken)
|
|
{
|
|
var authHeader = Request.Headers.Authorization.ToString();
|
|
var token = authHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)
|
|
? authHeader.Substring("Bearer ".Length).Trim()
|
|
: null;
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
return Unauthorized();
|
|
|
|
var user = await authService.GetUserByTokenAsync(token, cancellationToken);
|
|
if (user == null)
|
|
return Unauthorized();
|
|
|
|
return Ok(user);
|
|
}
|
|
}
|
|
|