public async Task <IActionResult> CreateUser([FromBody] NewUserResource newUser) { if (newUser.IsValidMail() == false) { return(BadRequest(new { message = "Mail did not pass Regex" })); } if (newUser.IsValidPassword() == false) { return(BadRequest(new { message = "Password did not pass Regex" })); } string salt = securityService.GenerateSalt(); string hashedPassword = securityService.HashPassword(newUser.Password, salt); User user = new User { Mail = newUser.Mail, Password = hashedPassword, PasswordSalt = salt, UserType = newUser.UserType }; if (user.UserType == UserType.Municipality) { string mailDomain = newUser.Mail.Substring(newUser.Mail.IndexOf('@') + 1); Municipality municipality = await municipalityService.GetMunicipalityByDomainAsync(mailDomain); if (municipality == null) { return(BadRequest("Invalid municipality domain given for municipality account!")); } user.MunicipalityName = municipality.Name; } else if (user.UserType == UserType.Admin) { return(BadRequest("You do not have permissions to create an admin account!")); } try { user = await usersService.AddNewUserAsync(user); } catch (DbUpdateException) { return(Conflict("User with the given mail already exists")); } PrivateSafeUserResource safeUser = mapper.Map <User, PrivateSafeUserResource>(user); if (user == null) { return(BadRequest(new { message = "Could not create user" })); } return(Created("Created new user successfully!", safeUser)); }
public async Task <IActionResult> Authenticate([FromBody] AuthUserResource authUserResource) { User user = await usersService.AuthenticateAsync(authUserResource.Mail, authUserResource.Password); if (user == null) { return(Unauthorized(new { message = "Incorrect mail or password" })); } PrivateSafeUserResource privateSafeUser = mapper.Map <User, PrivateSafeUserResource>(user); return(Ok(privateSafeUser)); }