34 lines
1 KiB
C#
34 lines
1 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Robware.Api.Auth.Models;
|
|
using Robware.Auth;
|
|
|
|
namespace Robware.Api.Auth.Controllers {
|
|
[ApiController]
|
|
public class AuthController : ControllerBase {
|
|
private readonly ILogger<AuthController> _logger;
|
|
private readonly IAuthenticator _authenticator;
|
|
|
|
public AuthController(ILogger<AuthController> logger, IAuthenticator authenticator) {
|
|
_logger = logger;
|
|
_authenticator = authenticator;
|
|
}
|
|
|
|
[HttpPost(nameof(Authenticate))]
|
|
public async Task<ActionResult<User>> Authenticate(LoginRequest request) {
|
|
var (result, user) = await _authenticator.Authenticate(request.Username, request.Password);
|
|
switch (result) {
|
|
case AuthenticationResult.Success:
|
|
return user;
|
|
case AuthenticationResult.NotFound:
|
|
return NotFound();
|
|
case AuthenticationResult.IncorrectPassword:
|
|
return Unauthorized();
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
}
|