public IActionResult LoginByPin([FromBody] LoginByPinCommand command) { try { string email = command.Email.ToLower(); string pin = command.Pin; string refreshToken = command.RefreshToken; LoginByPinDto loginByPinDto = _authService.LoginUser(email, pin, refreshToken); if (loginByPinDto == null) { return(BadRequest()); } string user = loginByPinDto.User; refreshToken = _authService.GetRefreshToken(email); string token = GetToken(email, user); LoginByPinViewModel model = new LoginByPinViewModel() { RefreshToken = refreshToken, Token = token, User = user, UserId = loginByPinDto.UserId, ShiftId = loginByPinDto.ShiftId }; return(Ok(model)); } catch (Exception e) { Console.WriteLine("Error : {0}", e.Message); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public LoginByPinDto LoginUser(string email, string pin, string refreshToken) { StoreEntity storeEntity = _storeRepository.GetStoreByEmail(email); UserEntity userEntity = _userRepository.GetUserByEmialAndPin(storeEntity.Id, pin); if (userEntity == null) { return(null); } TokenEntity tokenEntity = _tokenRepository.GetTokenByEmail(storeEntity.Id); if (tokenEntity == null) { return(null); } if (tokenEntity.RefreshToken != refreshToken) { return(null); } int userId = userEntity.Id; bool isUpdate = _tokenRepository.UpdateUserId(storeEntity.Id, userId); if (!isUpdate) { return(null); } ShiftEntity shiftEntity = _shiftRepository.GetShift(email, userId); int shiftId = 0; if (shiftEntity != null) { if (shiftEntity.Available == true) { shiftId = shiftEntity.Id; } } LoginByPinDto loginByPinDto = new LoginByPinDto() { User = userEntity.NameUser, UserId = userId, ShiftId = shiftId }; return(loginByPinDto); }