using System.Security.Claims; using Microsoft.AspNetCore.Identity; namespace MarcoBMS.Services.Helpers { public class UserHelper { private readonly UserManager _userManager; private readonly IHttpContextAccessor _httpContextAccessor; public UserHelper(UserManager userManager, IHttpContextAccessor httpContextAccessor) { _userManager = userManager; _httpContextAccessor = httpContextAccessor; } public int GetTenantId() { var tenant = _httpContextAccessor.HttpContext?.User.FindFirst("TenantId")?.Value; return (tenant != null ? Convert.ToInt32(tenant) : 1); } public async Task GetCurrentUserAsync() { var userId = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(userId)) return null; var user = await _userManager.FindByEmailAsync(userId); //var user = await _userManager.FindByNameAsync(userId); // For fetching user using username return user; //await _userManager.FindByIdAsync(userId); } public async Task GetCurrentUserProfileAsync() { var user = await GetCurrentUserAsync(); return user == null ? null : new { user.Id, user.UserName, user.Email, user.PhoneNumber }; } public async Task GetRegisteredUser(string email) { //IdentityUser? user = await _userManager.Users // .FirstOrDefaultAsync(u => u.Email == model.Email || u.PhoneNumber == model.PhoneNumber); IdentityUser? user = await _userManager.FindByEmailAsync(email); //if (user == null) //{ // user = await _userManager.Find(model.Email); //} return user; } } }