-
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
Showing
8 changed files
with
120 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Application.DTOs; | ||
|
||
public record UserLoginDto( | ||
string UserName, | ||
string Password | ||
); | ||
|
||
public record UserSignupDto( | ||
string Email, | ||
string UserName, | ||
string PhoneNumber, | ||
string Password | ||
); |
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
1 change: 1 addition & 0 deletions
1
src/Infrastructure/Interceptors/AuditableEntitySaveChangesInterceptor.cs
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
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 |
---|---|---|
@@ -1,37 +1,89 @@ | ||
using System.Security.Claims; | ||
using Application.DTOs; | ||
using Application.Services; | ||
using AutoMapper; | ||
using Domain.Models; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Presentation.Controllers; | ||
|
||
[Authorize] | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class UserController : ControllerBase | ||
{ | ||
//private readonly UserManager<User> _userManager; | ||
//private readonly RoleManager<IdentityRole> _roleManager; | ||
//private readonly IConfiguration _configuration; | ||
|
||
//public UserController(UserManager<User> userManager, RoleManager<IdentityRole> roleManager, IConfiguration configuration) | ||
//{ | ||
// _userManager = userManager; | ||
// _roleManager = roleManager; | ||
// _configuration = configuration; | ||
//} | ||
|
||
//[HttpGet("me")] | ||
//public async Task<IActionResult> GetCurrentUser() | ||
//{ | ||
// var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; | ||
// var user = await _userManager.FindByIdAsync(userId!); | ||
|
||
// if (user == null) | ||
// { | ||
// return NotFound(); | ||
// } | ||
|
||
// // Return user data | ||
// return Ok(user); | ||
//} | ||
private readonly UserManager<User> _userManager; | ||
private readonly RoleManager<IdentityRole> _roleManager; | ||
private readonly IAuthenticationService _authenticationService; | ||
private readonly IMapper _mapper; | ||
|
||
public UserController(UserManager<User> userManager, RoleManager<IdentityRole> roleManager, IAuthenticationService authenticationService, IMapper mapper) | ||
{ | ||
_userManager = userManager; | ||
_roleManager = roleManager; | ||
_authenticationService = authenticationService; | ||
_mapper = mapper; | ||
} | ||
|
||
[HttpGet("me")] | ||
public async Task<IActionResult> GetCurrentUser() | ||
{ | ||
var username = User.Identity!.Name; | ||
var user = await _userManager.FindByNameAsync(username!); | ||
|
||
if (user == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
// Return user data | ||
return Ok(user); | ||
} | ||
|
||
[HttpPost("login")] | ||
[AllowAnonymous] | ||
public async Task<ActionResult> Login([FromBody] UserLoginDto userLoginDto) | ||
{ | ||
var user = await _userManager.FindByNameAsync(userLoginDto.UserName); | ||
if (user != null && await _userManager.CheckPasswordAsync(user, userLoginDto.Password)) | ||
{ | ||
return Ok(new | ||
{ | ||
token = await _authenticationService.GenerateJwtToken(user), | ||
expiration = _authenticationService.GetTokenExpirationDays() | ||
}); | ||
} | ||
return Unauthorized(); | ||
} | ||
|
||
[HttpPost("signup")] | ||
[AllowAnonymous] | ||
public async Task<ActionResult> SignUp([FromBody] UserSignupDto userSignupDto) | ||
{ | ||
try | ||
{ | ||
var userExists = await _userManager.FindByNameAsync(userSignupDto.UserName); | ||
if (userExists != null) | ||
return Conflict("User already exists!"); | ||
|
||
var user = _mapper.Map<User>(userSignupDto); | ||
|
||
var result = await _userManager.CreateAsync(user, userSignupDto.Password); | ||
|
||
// if (!await _roleManager.RoleExistsAsync(UserRole.Customer)) | ||
// await _roleManager.CreateAsync(new IdentityRole(UserRole.Customer)); | ||
|
||
// await _userManager.AddToRoleAsync(user, UserRole.Customer); | ||
|
||
if (!result.Succeeded) | ||
return UnprocessableEntity("User creation failed! Please check user details and try again."); | ||
|
||
return Ok("User created successfully!"); | ||
} | ||
catch (Exception e) | ||
{ | ||
return BadRequest(e.Message); | ||
} | ||
} | ||
} |
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
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