public async Task <IActionResult> Register([FromBody] AfricanFarmersCommodities.Models.UserDetails userDetails) { if (userDetails.password != userDetails.repassword) { ModelState.AddModelError(string.Empty, "Password don't match"); return(BadRequest(new{ Error = "Passwords don't match", IsRegistered = false })); } if (string.IsNullOrEmpty(userDetails.mobileNumber)) { ModelState.AddModelError(string.Empty, "Mobile Number Required"); return(BadRequest(new { Error = "Mobile Number Required", IsRegistered = false })); } var newUser = new User { Username = userDetails.emailAddress, Email = userDetails.emailAddress, MobileNumber = userDetails.mobileNumber, FirstName = userDetails.firstName, LastName = userDetails.lastName, CreateTime = DateTime.Now, IsActive = false, IsLockedOut = false }; UserInteractionResults userCreationResult = await _userService.CreateAsync(newUser, userDetails.password); if (userCreationResult != UserInteractionResults.Succeeded) { ModelState.AddModelError(userCreationResult.ToString(), userCreationResult.ToString()); return(Ok(new { IsRegistered = false, ErrorMessage = userCreationResult.ToString() })); } return(Ok(new { IsRegistered = true, IsAdministrator = false, Message = UserInteractionResults.Succeeded.ToString() })); }
public async Task <IActionResult> Login([FromBody] AfricanFarmersCommodities.Models.UserDetails userDetails) { if (!string.IsNullOrEmpty(userDetails.authToken)) { var result = await Authenticate(userDetails.authToken); LoginResult res = JsonConvert.DeserializeObject <LoginResult>(JsonConvert.SerializeObject(result)); if (res.IsLoggedIn) { await SignInUserWithClaims(userDetails.emailAddress); return(Ok(res)); } } var signInResult = await CreateAuthoriseUsingLoginCredentials(userDetails); await SignInUserWithClaims(userDetails.emailAddress); return(signInResult); }
private async Task <IActionResult> CreateAuthoriseUsingLoginCredentials(AfricanFarmersCommodities.Models.UserDetails userDetails) { var user = await _userService.FindByEmailAsync(userDetails.emailAddress); if (user == null) { ModelState.AddModelError(string.Empty, "Invalid login"); return(BadRequest(new { IsLoggedIn = false, ErrorMessage = "Invalid login" })); } UserInteractionResults result = await _userService.PasswordSignInAsync(user, userDetails.password, isPersistent : userDetails.keepLoggedIn, lockoutOnFailure : false); if (result != UserInteractionResults.Succeeded) { ModelState.AddModelError(string.Empty, "Invalid login"); return(BadRequest(new { IsLoggedIn = false, ErrorMessage = "Invalid Login" })); } else if (result == UserInteractionResults.Succeeded) { var tmpUser = await _userService.FindByNameAsync(user.Username); var userRoles = await _roleService.FindByUserNameAsync(tmpUser.Username); var authToken = await _userService.AddUserRolesClaimAsync(tmpUser.Username, userRoles); tmpUser.Token = authToken; _unitOfWork.SaveChanges(); var isAdministrator = await _userService.IsUserInRoleAsync(user.Username.ToLower(), "administrator"); return(Ok(new { AuthToken = authToken, IsLoggedIn = true, IsAdministrator = isAdministrator })); } return(Ok(new { IsLoggedIn = false, IsAdministrator = false, Message = "Failed to Login!", Result = result.ToString() })); }