ShoppingApp/Weifer.ShoppingApp.API/RestApi/AuthenticationApiController.cs

46 lines
1.3 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using Weifer.Database.EF;
using Weifer.ShoppingApp.API.Controllers;
using Weifer.ShoppingApp.API.Models;
namespace Weifer.ShoppingApp.API.RestApi;
[ApiController]
[Route("api/[controller]")]
public class AuthenticationApiController : ControllerBase
2024-02-25 13:22:48 +01:00
{
public readonly DatabaseContext dbContext;
public readonly AuthenticationController authenticationController;
public AuthenticationApiController()
2024-02-25 13:22:48 +01:00
{
dbContext = new DatabaseContext();
authenticationController = new AuthenticationController();
}
[HttpPost("login")]
public async Task<IActionResult> CustomerLogin([FromBody] CustomerCredentials credentials)
{
if (credentials == null)
{
return Unauthorized();
}
var hashedPassword = dbContext.Customers.Where(cu => cu.Email == credentials.Email).Select(x => x.PasswordHash).FirstOrDefault();
if (hashedPassword == null)
{
return Unauthorized();
}
if (authenticationController.VerifyPassword(credentials.Password, hashedPassword))
{
var token = authenticationController.GenerateJwtToken();
return Ok(new { token = token });
}
return Unauthorized();
2024-02-25 13:22:48 +01:00
}
}