Added User Manager Extension
This commit is contained in:
parent
0586c201c5
commit
f68200d33e
@ -1,6 +1,7 @@
|
|||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using API.Dtos;
|
using API.Dtos;
|
||||||
using API.Errors;
|
using API.Errors;
|
||||||
|
using API.Extensions;
|
||||||
using Core.Entities.Identity;
|
using Core.Entities.Identity;
|
||||||
using Core.Interfaces;
|
using Core.Interfaces;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@ -25,8 +26,7 @@ namespace API.Controllers
|
|||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<UserDto>> GetCurrentUser()
|
public async Task<ActionResult<UserDto>> GetCurrentUser()
|
||||||
{
|
{
|
||||||
var email = User.FindFirstValue(ClaimTypes.Email);
|
var user = await _userManager.FindByEmailFromClaimsPrinciple(User);
|
||||||
var user = await _userManager.FindByEmailAsync(email);
|
|
||||||
return new UserDto
|
return new UserDto
|
||||||
{
|
{
|
||||||
Email = user.Email,
|
Email = user.Email,
|
||||||
@ -44,8 +44,7 @@ namespace API.Controllers
|
|||||||
[HttpGet("address")]
|
[HttpGet("address")]
|
||||||
public async Task<ActionResult<Address>> GetUserAddress()
|
public async Task<ActionResult<Address>> GetUserAddress()
|
||||||
{
|
{
|
||||||
var email = User.FindFirstValue(ClaimTypes.Email);
|
var user = await _userManager.FindUserByClaimsPrincipleWithAddressAsync(User);
|
||||||
var user = await _userManager.FindByEmailAsync(email);
|
|
||||||
return user.Address;
|
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