diff --git a/Controllers/LoginController.cs b/Controllers/LoginController.cs index 7423818..16157e6 100644 --- a/Controllers/LoginController.cs +++ b/Controllers/LoginController.cs @@ -99,6 +99,7 @@ namespace CarCareTracker.Controllers } else { _logger.LogInformation($"User {userEmailAddress} tried to login via OpenID but is not a registered user in LubeLogger."); + return View("OpenIDRegistration", model: userEmailAddress); } } } @@ -149,6 +150,12 @@ namespace CarCareTracker.Controllers return Json(result); } [HttpPost] + public IActionResult RegisterOpenIdUser(LoginModel credentials) + { + var result = _loginLogic.RegisterOpenIdUser(credentials); + return Json(result); + } + [HttpPost] public IActionResult RequestResetPassword(LoginModel credentials) { var result = _loginLogic.RequestResetPassword(credentials); diff --git a/Logic/LoginLogic.cs b/Logic/LoginLogic.cs index c4433ca..898374b 100644 --- a/Logic/LoginLogic.cs +++ b/Logic/LoginLogic.cs @@ -16,6 +16,7 @@ namespace CarCareTracker.Logic OperationResponse GenerateUserToken(string emailAddress, bool autoNotify); bool DeleteUserToken(int tokenId); bool DeleteUser(int userId); + OperationResponse RegisterOpenIdUser(LoginModel credentials); OperationResponse RegisterNewUser(LoginModel credentials); OperationResponse RequestResetPassword(LoginModel credentials); OperationResponse ResetPasswordByUser(LoginModel credentials); @@ -60,6 +61,45 @@ namespace CarCareTracker.Logic return result.Id != 0; } } + public OperationResponse RegisterOpenIdUser(LoginModel credentials) + { + //validate their token. + var existingToken = _tokenData.GetTokenRecordByBody(credentials.Token); + if (existingToken.Id == default || existingToken.EmailAddress != credentials.EmailAddress) + { + return new OperationResponse { Success = false, Message = "Invalid Token" }; + } + if (string.IsNullOrWhiteSpace(credentials.EmailAddress) || string.IsNullOrWhiteSpace(credentials.UserName)) + { + return new OperationResponse { Success = false, Message = "Username cannot be blank" }; + } + var existingUser = _userData.GetUserRecordByUserName(credentials.UserName); + if (existingUser.Id != default) + { + return new OperationResponse { Success = false, Message = "Username already taken" }; + } + var existingUserWithEmail = _userData.GetUserRecordByEmailAddress(credentials.EmailAddress); + if (existingUserWithEmail.Id != default) + { + return new OperationResponse { Success = false, Message = "A user with that email already exists" }; + } + _tokenData.DeleteToken(existingToken.Id); + var newUser = new UserData() + { + UserName = credentials.UserName, + Password = GetHash(NewToken()), //generate a password for OpenID User + EmailAddress = credentials.EmailAddress + }; + var result = _userData.SaveUserRecord(newUser); + if (result) + { + return new OperationResponse { Success = true, Message = "You will be redirected to the login page briefly." }; + } + else + { + return new OperationResponse { Success = false, Message = "Something went wrong, please try again later." }; + } + } //handles user registration public OperationResponse RegisterNewUser(LoginModel credentials) { diff --git a/Views/Login/OpenIDRegistration.cshtml b/Views/Login/OpenIDRegistration.cshtml new file mode 100644 index 0000000..630183b --- /dev/null +++ b/Views/Login/OpenIDRegistration.cshtml @@ -0,0 +1,50 @@ +@using CarCareTracker.Helper +@inject IConfigHelper config +@inject ITranslationHelper translator +@{ + var logoUrl = config.GetLogoUrl(); + var userLanguage = config.GetServerLanguage(); +} +@model string +@{ + ViewData["Title"] = "LubeLogger - Register"; +} +@section Scripts { + +} +