Added User Manager Extension
This commit is contained in:
parent
0586c201c5
commit
f68200d33e
@ -1,6 +1,7 @@
|
||||
using System.Security.Claims;
|
||||
using API.Dtos;
|
||||
using API.Errors;
|
||||
using API.Extensions;
|
||||
using Core.Entities.Identity;
|
||||
using Core.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@ -25,8 +26,7 @@ namespace API.Controllers
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<UserDto>> GetCurrentUser()
|
||||
{
|
||||
var email = User.FindFirstValue(ClaimTypes.Email);
|
||||
var user = await _userManager.FindByEmailAsync(email);
|
||||
var user = await _userManager.FindByEmailFromClaimsPrinciple(User);
|
||||
return new UserDto
|
||||
{
|
||||
Email = user.Email,
|
||||
@ -44,8 +44,7 @@ namespace API.Controllers
|
||||
[HttpGet("address")]
|
||||
public async Task<ActionResult<Address>> GetUserAddress()
|
||||
{
|
||||
var email = User.FindFirstValue(ClaimTypes.Email);
|
||||
var user = await _userManager.FindByEmailAsync(email);
|
||||
var user = await _userManager.FindUserByClaimsPrincipleWithAddressAsync(User);
|
||||
return user.Address;
|
||||
|
||||
}
|
||||
|
23
API/Extensions/UserManagerExtensions.cs
Normal file
23
API/Extensions/UserManagerExtensions.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Security.Claims;
|
||||
using Core.Entities.Identity;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Extensions
|
||||
{
|
||||
public static class UserManagerExtensions
|
||||
{
|
||||
public static async Task<AppUser> FindUserByClaimsPrincipleWithAddressAsync(this UserManager<AppUser> input, ClaimsPrincipal user)
|
||||
{
|
||||
var email = user.FindFirstValue(ClaimTypes.Email);
|
||||
return await input.Users.Include(x => x.Address).SingleOrDefaultAsync(x => x.Email == email);
|
||||
|
||||
}
|
||||
|
||||
public static async Task<AppUser> FindByEmailFromClaimsPrinciple(this UserManager<AppUser> input, ClaimsPrincipal user)
|
||||
{
|
||||
var email = user.FindFirstValue(ClaimTypes.Email);
|
||||
return await input.Users.SingleOrDefaultAsync(x => x.Email == email);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user