public ActionResult <List <string> > PostUser([FromBody] UserRegister user) { if (!InputValidationOperations.ArePasswordsEqual(user.Password, user.ConfirmPassword)) { return(BadRequest(new Error("Passwords don't match"))); } if (!UserExists(Cryptography.HashString(user.Email))) { User newUser = new User(); newUser.UserName = user.UserName; newUser.Email = user.Email; newUser.Password = user.Password; newUser.Administrator = true; newUser = Cryptography.HashUserData(newUser); _context.Add(newUser); _context.SaveChanges(); List <string> jsonResponse = new List <string>(); jsonResponse.Add(JWT.CreateJWT(newUser.Id, 1)); jsonResponse.Add(user.UserName); return(jsonResponse); } else { return(Conflict(new Error("Email already exists"))); } }
public ActionResult <List <string> > LoginUser([FromBody] Credentials userCredentials) { string hashedMail = Cryptography.HashString(userCredentials.Email); string hashedPassword = Cryptography.HashString(userCredentials.Password); List <User> user = _context.Users.Where(u => u.Email == Cryptography.HashString(userCredentials.Email) && u.Password == Cryptography.HashString(userCredentials.Password)).ToList(); if (user.Count != 0) { List <string> response = new List <string>(); response.Add(JWT.CreateJWT(user[0].Id, 1)); response.Add(user[0].UserName); return(response); } else { return(BadRequest(new Error("Email or password invalid"))); } }