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 { public readonly DatabaseContext dbContext; public readonly AuthenticationController authenticationController; public AuthenticationApiController() { dbContext = new DatabaseContext(); authenticationController = new AuthenticationController(); } [HttpPost("login")] public async Task 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(); } }