using Api.SeaHavenIndustries.DTOs; using Api.SeaHavenIndustries.Helper; using Data.SeaHavenIndustries; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using SeaHaven.Services.DTOs; using SeaHaven.Services.Interfaces; using System.Security.Claims; namespace Api.SeaHavenIndustries.Controllers { [ApiController] [Route("api/Authentication")] public class AuthenticationController : Controller { private readonly IAuthenticationService _authenticationService; private readonly ILogger _logger; public AuthenticationController(IAuthenticationService authenticationService, ILogger logger) { _authenticationService = authenticationService; _logger = logger; } [AllowAnonymous] [HttpPost] [Route("login")] public async Task Login([FromBody] LoginModel model, CancellationToken cancellationToken) { try { var result = await _authenticationService.LoginAsync(model.Username, model.Password, cancellationToken); if (result != null) { return Ok(new { token = result.Token, expiration = result.Expiration, email = result.Email, userRoles = result.UserRole, phoneNumber = result.PhoneNumber, fullname = result.Fullname, id = result.Id }); } return Unauthorized(); } catch (Exception ex) { return StatusCode(500, _logger.Sanitize(ex)); } } [Route("ChangePassword")] [HttpPost] public async Task ChangePassword(ChangePasswords usermodel, CancellationToken cancellationToken) { var userid = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? ""; var succeeded = await _authenticationService.ChangePasswordAsync(userid, usermodel.Currentpassword, usermodel.Confirmpassword, cancellationToken); if (succeeded) { return Ok(new Response { Status = "Success ", Message = "Password successfully changed" }); } else return BadRequest(new Response { Status = "Old Password is incorrect" }); } [HttpPost] [Route("UpdateProfile")] public async Task UserProfileUpdate([FromForm] User_DTO model, CancellationToken cancellationToken) { try { var userid = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? ""; var dto = new UpdateProfileRequestDTO { Name = model.Name, Email = model.Email, Contact = model.Contact }; var data = await _authenticationService.UpdateProfileAsync(userid, dto, cancellationToken); return Ok(new DataResponse { Message = "Introduction Updated Successfully", Status = "200", Data = data == null ? null : new { data.FirstName, data.Email, data.Contact } }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } #region Forget Password Area [AllowAnonymous] [HttpPost()] [Route("ForgetPassword")] public async Task ForgetPassword(string Email, CancellationToken cancellationToken) { var found = await _authenticationService.ForgetPasswordAsync(Email, cancellationToken); if (found) { return Ok(new Response { Status = "Success ", Message = "Please check your email for code" }); } else { return BadRequest(new Response { Status = "Error", Message = "No such email is registered" }); } } //need email and code [AllowAnonymous] [HttpPost()] [Route("VerificationCode")] public async Task VerificationCode(string code, CancellationToken cancellationToken) { try { if (await _authenticationService.VerifyCodeAsync(code, cancellationToken)) { return Ok(new Response { Status = "Success ", Message = "Code Matched" }); } else { return BadRequest(new Response { Status = "Error", Message = "Code Not Matched" }); } } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } // need email, password and code [AllowAnonymous] [HttpPost()] [Route("ResetPassword")] public async Task ResetPassword(ForgetPassword_Dto fpdto, CancellationToken cancellationToken) { try { if (await _authenticationService.ResetPasswordAsync(fpdto.Email, fpdto.Code, fpdto.Password, cancellationToken)) { return Ok(new Response { Status = "Success ", Message = "password changed" }); } else { return BadRequest(new Response { Status = "Error", Message = "Your email or code not found please check" }); } } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } #endregion } }