public IActionResult Register([FromBody] Rider rider) { try { //if (ModelState.IsValid) { if (_service.UserNameExists(rider.UserName)) { return(Unauthorized()); } // A valid new rider to be added AuthorizationResponseData ResponseData = _service.AddRiderWAuthorization(rider); return(Ok(ResponseData)); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(BadRequest("Unable to save changes. Try again.")); }
public AuthorizationResponseData UpdateRiderProfile(Rider rider, UpdateRiderRequestData requestData) { AuthorizationResponseData ResponseData = new AuthorizationResponseData(); rider.FirstName = requestData.FirstName; rider.LastName = requestData.LastName; // only change the role if found in the request. if (requestData.Role != "") { rider.Role = requestData.Role; } // not going to allow anyone to change username!!! rider.PhoneNumber = requestData.PhoneNumber; rider.EmailAddress = requestData.EmailAddress; // save to database _context.Riders.Update(rider); _context.SaveChanges(); // set up the response data ResponseData.UserId = rider.ID; ResponseData.FirstName = rider.FirstName; ResponseData.UserName = rider.UserName; ResponseData.Role = rider.Role; ResponseData.Authorization = Auth.GenerateJWT(rider); return(ResponseData); }
public AuthorizationResponseData UpdatePassword(Rider rider, string NewPassword) { AuthorizationResponseData ResponseData = new AuthorizationResponseData(); rider.Password = Auth.Hash(NewPassword, rider.Salt); _context.Riders.Update(rider); _context.SaveChanges(); ResponseData.UserId = rider.ID; ResponseData.FirstName = rider.FirstName; ResponseData.UserName = rider.UserName; ResponseData.Role = rider.Role; ResponseData.Authorization = Auth.GenerateJWT(rider); return(ResponseData); }
public IActionResult EditRider([FromBody] UpdateRiderRequestData RequestData) { if (!_service.IsAuthorizedRiderOrAdmin( RequestData.RequestingId, RequestData.TargetId, RequestData.Authorization)) { return(Unauthorized()); } var riderToUpdate = _context.Riders.SingleOrDefault(r => r.ID == RequestData.TargetId); if (riderToUpdate != null) { AuthorizationResponseData ResponseData = _service.UpdateRiderProfile(riderToUpdate, RequestData); return(Ok(ResponseData)); } return(NotFound()); }
public IActionResult Login([FromBody] LoginRequestData user) { User foundUser = _context.Riders.SingleOrDefault <Rider>( r => r.UserName == user.UserName && r.Password == Auth.Hash(user.Password, r.Salt) ); if (foundUser != null) { AuthorizationResponseData ResponseData = new AuthorizationResponseData(); ResponseData.UserId = foundUser.ID; ResponseData.Authorization = Auth.GenerateJWT(foundUser); ResponseData.FirstName = foundUser.FirstName; ResponseData.UserName = foundUser.UserName; ResponseData.Role = foundUser.Role; return(Ok(ResponseData)); } return(NotFound()); }
public IActionResult ChangePassword([FromBody] ChangePasswordRequest RequestData) { if (!_service.IsAuthorizedRiderOrAdmin( RequestData.RequestingId, RequestData.TargetId, RequestData.Authorization)) { return(Unauthorized()); } Rider foundRider = _context.Riders.SingleOrDefault <Rider>( r => r.ID == RequestData.TargetId); if (foundRider != null) { AuthorizationResponseData ResponseData = _service.UpdatePassword(foundRider, RequestData.Password); return(Ok(ResponseData)); } return(NotFound()); }
public AuthorizationResponseData AddRiderWAuthorization(Rider newRider) { AuthorizationResponseData ResponseData = new AuthorizationResponseData(); /* * Rider newRider = new Rider(); * newRider.LastName = rider.LastName; * newRider.Password = rider.Password; * newRider.FirstName = rider.FirstName; * newRider.UserName = rider.UserName; * newRider.PhoneNumber = rider.PhoneNumber; * newRider.EmailAddress = rider.EmailAddress; */ AddRider(newRider); Rider riderToGet = _context.Riders.SingleOrDefault(r => r.UserName == newRider.UserName); ResponseData.UserId = riderToGet.ID; ResponseData.FirstName = riderToGet.FirstName; ResponseData.UserName = riderToGet.UserName; ResponseData.Role = riderToGet.Role; ResponseData.Authorization = Auth.GenerateJWT(newRider); return(ResponseData); }