public async Task <IActionResult> CompanySignIn([FromBody] UserLoginDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { return(NotFound($"User with email {model.Email} not found")); } var roles = await _userManager.GetRolesAsync(user); if (!roles.Contains("Company")) { return(Unauthorized()); } var signInResult = await _signInManager.PasswordSignInAsync(user, model.Password, true, false); if (!signInResult.Succeeded) { return(Unauthorized("Wrong password")); } var dtoToReturn = await _companyRepository.GetById(user.Id); return(dtoToReturn == null ? (IActionResult)BadRequest($"User with id {user.Id} could not be signed in") : Ok(dtoToReturn)); }
private async Task <UserDto> ReturnUserFromRoleAsync(ApplicationUser user) { var role = (await _userManager.GetRolesAsync(user)).First(); var dtoToReturn = role.ToUpper() switch { "CUSTOMER" => (UserDto)await _customerRepository.GetById(user.Id), "DRIVER" => await _driverRepository.GetById(user.Id), "COMPANY" => await _companyRepository.GetById(user.Id), _ => throw new ArgumentOutOfRangeException() }; dtoToReturn.Email = user.Email; dtoToReturn.PhoneNumber = user.PhoneNumber; return(dtoToReturn); } }