public IActionResult Register([FromBody] UserCredentials userCredentials) { if (userCredentials == null || string.IsNullOrWhiteSpace(userCredentials.Username) || string.IsNullOrWhiteSpace(userCredentials.Password)) { return(new Responses.BadRequestResult("Invalid data,")); } using (var context = new SkoPaTuManaContext()) { if (UserExists(userCredentials, context)) { return(new Responses.BadRequestResult("User already registered,")); } context.Users.Add(new Users { Username = userCredentials.Username, Password = userCredentials.Password }); context.SaveChanges(); return(new Responses.OkResult("User created,")); } }
public IActionResult Login([FromBody] UserCredentials userCredentials) { if (userCredentials == null || string.IsNullOrWhiteSpace(userCredentials.Username) || string.IsNullOrWhiteSpace(userCredentials.Password)) { return(new Responses.BadRequestResult("Invalid credentials,")); } using (var context = new SkoPaTuManaContext()) { var user = GetUser(userCredentials, context); if (user == null) { return(new Responses.BadRequestResult("User does not exist,")); } if (user.Password == userCredentials.Password) { return(new Responses.OkResult("Login successful,")); } return(new Responses.BadRequestResult("Wrong password,")); } }
private static Users GetUser(UserCredentials userCredentials, SkoPaTuManaContext context) { return(context.Users.FirstOrDefault(u => u.Username == userCredentials.Username)); }
private static bool UserExists(UserCredentials userCredentials, SkoPaTuManaContext context) { return(context.Users.Any(u => u.Username == userCredentials.Username)); }