-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
053399f
commit 56de9ed
Showing
7 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using BHC24.Api.Persistence.Models; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.IdentityModel.Tokens; | ||
using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames; | ||
|
||
namespace BHC24.Api.Controllers; | ||
|
||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class AuthController : ControllerBase | ||
{ | ||
private readonly UserManager<AppUser> _userManager; | ||
private readonly IConfiguration _configuration; | ||
|
||
public AuthController(UserManager<AppUser> userManager, IConfiguration configuration) | ||
{ | ||
_userManager = userManager; | ||
_configuration = configuration; | ||
} | ||
|
||
[HttpPost("register")] | ||
public async Task<IActionResult> Register([FromBody] RegisterRequest request) | ||
{ | ||
var user = new AppUser { UserName = request.Email, Email = request.Email }; | ||
var result = await _userManager.CreateAsync(user, request.Password); | ||
if (result.Succeeded) | ||
{ | ||
return Ok(); | ||
} | ||
return BadRequest(result.Errors); | ||
} | ||
|
||
[HttpPost("login")] | ||
public async Task<IActionResult> Login([FromBody] LoginRequest request) | ||
{ | ||
var user = await _userManager.FindByEmailAsync(request.Email); | ||
if (user != null && await _userManager.CheckPasswordAsync(user, request.Password)) | ||
{ | ||
var token = GenerateJwtToken(user); | ||
return Ok(new { Token = token }); | ||
} | ||
return Unauthorized(); | ||
} | ||
|
||
private string GenerateJwtToken(AppUser user) | ||
{ | ||
var claims = new[] | ||
{ | ||
new Claim(JwtRegisteredClaimNames.Sub, user.UserName), | ||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | ||
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()) | ||
}; | ||
|
||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"])); | ||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); | ||
|
||
var token = new JwtSecurityToken( | ||
issuer: _configuration["Jwt:Issuer"], | ||
audience: _configuration["Jwt:Audience"], | ||
claims: claims, | ||
expires: DateTime.Now.AddDays(1), | ||
signingCredentials: creds); | ||
|
||
return new JwtSecurityTokenHandler().WriteToken(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BHC24.Api.Controllers; | ||
|
||
public class LoginRequest | ||
{ | ||
public string Email { get; set; } | ||
public string Password { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BHC24.Api.Controllers; | ||
|
||
public class RegisterRequest | ||
{ | ||
public string Email { get; set; } | ||
public string Password { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Security.Claims; | ||
using BHC24.Api.Persistence.Models; | ||
using Microsoft.AspNetCore.Identity; | ||
|
||
namespace BHC24.Api.Services; | ||
|
||
public class AuthUserProvider | ||
{ | ||
private readonly UserManager<AppUser> _userManager; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public AuthUserProvider(UserManager<AppUser> userManager, IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_userManager = userManager; | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public async Task<AppUser> GetAsync() | ||
{ | ||
string? userEmail = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); | ||
if (string.IsNullOrEmpty(userEmail)) | ||
{ | ||
throw new InvalidOperationException("User is not authenticated"); | ||
} | ||
|
||
AppUser? user = await _userManager.FindByEmailAsync(userEmail); | ||
if (user == null) | ||
{ | ||
throw new InvalidOperationException("User not found"); | ||
} | ||
|
||
return user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters