-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.cs
More file actions
45 lines (40 loc) · 1.86 KB
/
Copy pathAuthController.cs
File metadata and controls
45 lines (40 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Voltiq.Application.Features.Auth.Commands.Login;
using Voltiq.Application.Features.Users.Queries.GetCurrentUser;
using Voltiq.Application.Mappings.Auth;
namespace Voltiq.API.Controllers.Auth;
[Route("auth")]
public sealed class AuthController : BaseApiController
{
/// <summary>Authenticates a user and returns a JWT token.</summary>
/// <response code="200">Authentication successful.</response>
/// <response code="400">Validation error.</response>
/// <response code="401">Invalid credentials.</response>
[AllowAnonymous]
[HttpPost("login")]
[ProducesResponseType(typeof(LoginResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> Login(
[FromBody] LoginRequest request,
CancellationToken cancellationToken)
{
var command = request.ToCommand();
var result = await Sender.Send(command, cancellationToken);
return result.IsFailure ? ToErrorResult(result) : Ok(result.Value);
}
/// <summary>Returns the currently authenticated user.</summary>
/// <response code="200">Current user data.</response>
/// <response code="401">Token missing or invalid.</response>
/// <response code="404">User no longer exists.</response>
[HttpGet("me")]
[ProducesResponseType(typeof(GetUserResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Me(CancellationToken cancellationToken)
{
var result = await Sender.Send(new GetCurrentUserQuery(), cancellationToken);
return result.IsFailure ? ToErrorResult(result) : Ok(result.Value);
}
}