public async Task <UserViewModel> GetCurrentUser(Guid token) { UserModel user = await _context.Users.FirstOrDefaultAsync(x => x.Token == token && x.IsActive); if (user != null) { return(UserConverter.ConvertDbModelToViewModel(user)); } else { throw new NullReferenceException(); } }
public async Task <UserViewModel> Login(string email, string password) { UserModel user = await _context.Users.FirstOrDefaultAsync(x => x.Email.ToLower() == email.ToLower() && x.Password == password && x.IsActive); if (user != null) { if (user.IsAccountActivated == false) { InvalidOperationException ex = new InvalidOperationException(); ex.Data.Add("message", "ACCOUNT_NOT_ACTIVATED"); throw ex; } if (user.SystemAccess == false) { InvalidOperationException ex = new InvalidOperationException(); ex.Data.Add("message", "ACCOUNT_BLOCKED"); throw ex; } user.IsLoggedIn = true; HistoryModel historyRow = new HistoryModel() { Description = "USER_LOGGED_INTO_SYSTEM", UserData = user.Name + " " + user.Surname }; _context.Users.Update(user); _context.History.Add(historyRow); await _context.SaveChangesAsync(); return(UserConverter.ConvertDbModelToViewModel(user)); } else { InvalidOperationException ex = new InvalidOperationException(); ex.Data.Add("message", "LOGIN_DATA_INCORRECT"); throw ex; } }