public IActionResult Login([FromBody] LoginModelIn login) { if (ModelState.IsValid) { try { Guid token = sessionLogic.Login(login.Mail, login.Password); if (token == null) { return(BadRequest("You have entered a wrong mail or password. Try again!")); } UserEntity admin = userLogic.GetByMail(login.Mail); return(Ok(new LoginModelOut { Token = token, Mail = admin.Mail, IsAdmin = admin.IsAdmin })); } catch (ArgumentException e) { return(Unauthorized(e.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } else { return(Unauthorized(ModelState)); } }
public void Login_ExpectsOk() { unitOfWork.Setup(x => x.UserRepository.FirstOrDefault(It.IsAny <Expression <Func <UserEntity, bool> > >())).Returns(adminEntity); sessionRepository.Setup(u => u.Add(It.IsAny <SessionEntity>())).Verifiable(); var guid = sessionLogic.Login(adminEntity.Mail, adminEntity.Password); SessionEntity sessionEntity = new SessionEntity { Token = guid, Mail = adminEntity.Mail }; sessionRepository.Setup(x => x.FirstOrDefault(It.IsAny <Expression <Func <SessionEntity, bool> > >())).Returns(sessionEntity); var isValid = sessionLogic.IsValidToken(guid); sessionRepository.VerifyAll(); Assert.IsTrue(isValid); }
public IActionResult Login([FromBody] SessionModel sessionModel) { try { Guid token = sessionLogic.Login(sessionModel.username, sessionModel.password); if (token == null) { return(BadRequest(sessionModel)); } LogLogic ll = new LogLogic(null); Log log = new Log { Username = sessionModel.username, Date = DateTime.Now, Accion = "Login" }; ll.AddRegister(log); return(Ok(token)); } catch (ArgumentException exception) { return(BadRequest("Error " + sessionModel.username + " " + sessionModel.password + " -> " + exception.Message)); } }