Add checks on the login and creating of user
This commit is contained in:
@@ -8,6 +8,6 @@ namespace skydiveLogs_api.Business.Interface
|
|||||||
|
|
||||||
User GetById(int userId);
|
User GetById(int userId);
|
||||||
|
|
||||||
void AddNewUser(User user);
|
bool AddNewUser(User user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,10 +27,19 @@ namespace skydiveLogs_api.Business
|
|||||||
return _userRepository.GetByLogin(login, EncryptPassword(password));
|
return _userRepository.GetByLogin(login, EncryptPassword(password));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddNewUser(User newUser)
|
public bool AddNewUser(User newUser)
|
||||||
{
|
{
|
||||||
newUser.Password = EncryptPassword(newUser.Password);
|
newUser.Password = EncryptPassword(newUser.Password);
|
||||||
_userRepository.Add(newUser);
|
var foundUser = _userRepository.GetByLogin(newUser.Login, newUser.Password);
|
||||||
|
var result = false;
|
||||||
|
|
||||||
|
if (foundUser == null)
|
||||||
|
{
|
||||||
|
_userRepository.Add(newUser);
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string EncryptPassword(string password)
|
private string EncryptPassword(string password)
|
||||||
|
|||||||
@@ -72,9 +72,26 @@ namespace skydiveLogs_api.Controllers
|
|||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[EnableCors]
|
[EnableCors]
|
||||||
public void Post([FromBody] UserReq value)
|
public IActionResult Post([FromBody] UserReq userToAdd)
|
||||||
{
|
{
|
||||||
_userService.AddNewUser(_mapper.Map<User>(value));
|
IActionResult result;
|
||||||
|
var newUser = _mapper.Map<User>(userToAdd);
|
||||||
|
var userAdded = _userService.AddNewUser(newUser);
|
||||||
|
|
||||||
|
if (!userAdded)
|
||||||
|
{
|
||||||
|
result = BadRequest(new { message = "Error during the creation of the user." });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newUser.Password = null;
|
||||||
|
var resp = _mapper.Map<UserResp>(newUser);
|
||||||
|
resp.Token = CreateToken(resp);
|
||||||
|
|
||||||
|
result = Ok(resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string CreateToken(UserResp foundUser)
|
private string CreateToken(UserResp foundUser)
|
||||||
|
|||||||
Reference in New Issue
Block a user