Untitled
unknown
plain_text
2 years ago
1.6 kB
18
Indexable
public async Task<UserDto> GetCurrentuser()
{
var user = await GetCurrentUserAsync();
if (user is { })
{
var userDto = _mapper.Map<UserDto>(user);
userDto.Token = await _tokenService.CreateTokenAsync(user);
userDto.DisplayName = user.UserName!;
return userDto;
}
else
{
return null;
}
}
private async Task<AppUser> GetCurrentUserAsync()
{
var userIdClaim = _httpContextAccessor?.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier);
if (userIdClaim == null) return null;
var userId = userIdClaim.Value;
if (string.IsNullOrEmpty(userId)) return null;
var user=await _userManager.FindByIdAsync(userId);
if(user is not null)
return user;
return null;
}
-------------------------------------------------------------------------
public interface IUserRepositroy
{
Task<UserDto> GetCurrentuser();
}
----------------------------------------------
[Authorize]
[HttpGet("GetCurrentuser")]
public async Task<ActionResult<UserDto>> GetCurrentuser()
{
var user = await _userRepositroy.GetCurrentuser();
if (user is null)
{
return NotFound(new ApiResponse(404, "User not found"));
}
return Ok(user);
}
---------------------------------------------------------Editor is loading...
Leave a Comment