Skip to content

Commit a4bbd9e

Browse files
authored
Merge pull request #19 from anycode-pk/GetUserInfo
GetUserInfo endpoint
2 parents ae29d64 + 7686acd commit a4bbd9e

File tree

5 files changed

+65
-8
lines changed

5 files changed

+65
-8
lines changed

TableBooking.Api/Controllers/UserController.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace TableBooking.Controllers
88
{
9+
using Model.Models;
10+
911
[Route("[controller]")]
1012
[ApiController]
1113
[Authorize]
@@ -35,10 +37,26 @@ public async Task<IActionResult> Login([FromBody] UserLoginDto userLoginDTO)
3537
}
3638

3739
[HttpPost]
40+
[Authorize]
3841
[Route("logout")]
3942
public async Task<IActionResult> Logout()
4043
{
44+
// TODO: implement logout
4145
throw new NotImplementedException();
4246
}
47+
48+
[HttpGet]
49+
[Authorize]
50+
public async Task<AppUserDto> GetUserInfo()
51+
{
52+
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
53+
54+
if (userId == null)
55+
{
56+
throw new UnauthorizedAccessException("User is not authenticated.");
57+
}
58+
59+
return await _userService.GetUserInfo(Guid.Parse(userId), CancellationToken.None);
60+
}
4361
}
4462
}

TableBooking.Api/Interfaces/IUserService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
namespace TableBooking.Api.Interfaces
77
{
8+
using Model.Models;
9+
810
public interface IUserService
911
{
1012
public Task<IActionResult> Register(UserRegisterDto userRegisterDTO);
1113
public Task<IActionResult> Login(UserLoginDto userLoginDTO);
14+
public Task<AppUserDto> GetUserInfo(Guid id, CancellationToken cancellationToken);
1215
public Task SeedRoles();
1316
}
1417
}

TableBooking.Api/Services/UserService.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@
1212

1313
namespace TableBooking.Api.Services
1414
{
15+
using Microsoft.EntityFrameworkCore;
16+
using Model;
17+
1518
public class UserService : IUserService
1619
{
1720
private readonly UserManager<AppUser> _userManager;
1821
private readonly RoleManager<AppRole> _roleManager;
1922
private readonly IConfiguration _configuration;
2023
private readonly string userRoleId = "5ad1268f-f61f-4b1c-b690-cbf8c3d35019";
24+
private readonly TableBookingContext _dbContext;
2125

2226
public UserService(
2327
UserManager<AppUser> userManager,
2428
RoleManager<AppRole> roleManager,
2529
IConfiguration configuration,
26-
IUnitOfWork unitOfWork)
30+
IUnitOfWork unitOfWork,
31+
TableBookingContext dbContext)
2732
{
2833
_userManager = userManager;
2934
_roleManager = roleManager;
3035
_configuration = configuration;
36+
_dbContext = dbContext;
3137
}
3238

3339
public async Task<IActionResult> Register(UserRegisterDto dto)
@@ -82,6 +88,15 @@ public async Task<IActionResult> Login(UserLoginDto dto)
8288
});
8389
}
8490

91+
public async Task<AppUserDto> GetUserInfo(Guid id, CancellationToken cancellationToken)
92+
{
93+
var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == id, cancellationToken);
94+
95+
var userDto = user?.ToDto();
96+
97+
return userDto ?? new();
98+
}
99+
85100
private JwtSecurityToken GetToken(List<Claim> authClaims)
86101
{
87102
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace TableBooking.Model.Dtos.UserDtos;
2+
3+
using Models;
4+
5+
public class AppUserDto
6+
{
7+
public IEnumerable<Booking> Bookings { get; set; }
8+
public string? Email { get; set; } = string.Empty;
9+
public string? Username { get; set; } = string.Empty;
10+
}
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
using Microsoft.AspNetCore.Identity;
22

3-
namespace TableBooking.Model.Models
3+
namespace TableBooking.Model.Models;
4+
5+
using Dtos.UserDtos;
6+
7+
public class AppUser : IdentityUser<Guid>
48
{
5-
public class AppUser : IdentityUser<Guid>
9+
public string? RefreshToken { get; set; }
10+
public DateTime? RefreshTokenExpiryTime { get; set; }
11+
public IEnumerable<Booking> Bookings { get; set; }
12+
public Guid AppRoleId { get; set; }
13+
public AppRole AppRole { get; set; }
14+
15+
public AppUserDto ToDto()
616
{
7-
public string? RefreshToken { get; set; }
8-
public DateTime? RefreshTokenExpiryTime { get; set; }
9-
public IEnumerable<Booking> Bookings { get; set; }
10-
public Guid AppRoleId { get; set; }
11-
public AppRole AppRole { get; set; }
17+
return new AppUserDto
18+
{
19+
Bookings = Bookings,
20+
Email = this.Email,
21+
Username = this.UserName
22+
};
1223
}
1324
}

0 commit comments

Comments
 (0)