using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using MyPlaceAPI.Data; namespace MyPlaceAPI.Controllers; public class ProfileController : ControllerBase { private readonly UserManager _userManager; private readonly SignInManager _signInManager; private readonly IConfiguration _configuration; public ProfileController( UserManager userManager, SignInManager signInManager, IConfiguration configuration) { _userManager = userManager; _signInManager = signInManager; _configuration = configuration; } [HttpPost("register")] public async Task Register([FromBody] ProfileDTO profile) { if (profile.Email is null || profile.Password is null || profile.FirstName is null || profile.LastName is null || profile.UserName is null) { return BadRequest(); } Profile newProfile = new Profile { Email = profile.Email, FirstName = profile.FirstName, LastName = profile.LastName, UserName = profile.UserName }; var result = await _userManager.CreateAsync(newProfile, profile.Password); if (!result.Succeeded) { return BadRequest(result.Errors); } return Ok(); } [HttpPost("login")] public async Task> Login([FromBody] ProfileDTO profile) { if (profile.Email is null || profile.Password is null) { return BadRequest(); } Profile? user = await _userManager.FindByEmailAsync(profile.Email); if (user is null) return Unauthorized(); var result = await _signInManager.CheckPasswordSignInAsync(user, profile.Password, false); if (!result.Succeeded) return Unauthorized(); return GenerateJwtToken(user); } private string GenerateJwtToken(Profile profile) { var claims = new List { new Claim(ClaimTypes.Name, profile.FirstName) }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Tokens:Key"]!)); var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var tokenDescriptor = new JwtSecurityToken( issuer: _configuration["Tokens:Issuer"], audience: _configuration["Tokens:Audience"], claims: claims, expires: DateTime.Now.AddMinutes(30), signingCredentials: credentials); var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.WriteToken(tokenDescriptor); return token; } }