register validation
This commit is contained in:
		| @ -7,52 +7,54 @@ | ||||
|  | ||||
| @if (PageState.Site.AllowRegistration) | ||||
| { | ||||
|     <AuthorizeView Roles="@RoleNames.Registered"> | ||||
|         <Authorizing> | ||||
|             <text>...</text> | ||||
|         </Authorizing> | ||||
|         <Authorized> | ||||
|             <ModuleMessage Message="@Localizer["Info.Registration.Exists"]" Type="MessageType.Info" /> | ||||
|         </Authorized> | ||||
|         <NotAuthorized> | ||||
|             <ModuleMessage Message="@Localizer["Info.Registration.InvalidEmail"]" Type="MessageType.Info" /> | ||||
|             <div class="container"> | ||||
|                 <div class="row mb-1 align-items-center"> | ||||
|                     <Label Class="col-sm-3" For="username" HelpText="Your username. Note that this field can not be modified once it is saved." ResourceKey="Username"></Label> | ||||
|                     <div class="col-sm-9"> | ||||
|                         <input id="username" class="form-control" @bind="@_username"  /> | ||||
|     <form @ref="form" class="@(validated ? "was-validated" : "needs-validation")" novalidate> | ||||
|         <AuthorizeView Roles="@RoleNames.Registered"> | ||||
|             <Authorizing> | ||||
|                 <text>...</text> | ||||
|             </Authorizing> | ||||
|             <Authorized> | ||||
|                 <ModuleMessage Message="@Localizer["Info.Registration.Exists"]" Type="MessageType.Info" /> | ||||
|             </Authorized> | ||||
|             <NotAuthorized> | ||||
|                 <ModuleMessage Message="@Localizer["Info.Registration.InvalidEmail"]" Type="MessageType.Info" /> | ||||
|                 <div class="container"> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="username" HelpText="Your username. Note that this field can not be modified once it is saved." ResourceKey="Username"></Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input id="username" class="form-control" @bind="@_username" maxlength="256" required /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="password" HelpText="If you wish to change your password you can enter it here. Please choose a sufficiently secure password." ResourceKey="Password"></Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input id="password" type="password" class="form-control" @bind="@_password" autocomplete="new-password" required /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="confirm" HelpText="If you are changing your password you must enter it again to confirm it matches" ResourceKey="Confirm"></Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input id="confirm" type="password" class="form-control" @bind="@_confirm" autocomplete="new-password" required /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="email" HelpText="Your email address where you wish to receive notifications" ResourceKey="Email"></Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input id="email" class="form-control" @bind="@_email" maxlength="256" required /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="displayname" HelpText="Your full name" ResourceKey="DisplayName"></Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input id="displayname" class="form-control" @bind="@_displayname" maxlength="50" required /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                 </div> | ||||
|                 <div class="row mb-1 align-items-center"> | ||||
|                     <Label Class="col-sm-3" For="password" HelpText="If you wish to change your password you can enter it here. Please choose a sufficiently secure password." ResourceKey="Password"></Label> | ||||
|                     <div class="col-sm-9"> | ||||
|                         <input id="password" type="password" class="form-control" @bind="@_password" autocomplete="new-password" /> | ||||
|                     </div> | ||||
|                 </div> | ||||
|                 <div class="row mb-1 align-items-center"> | ||||
|                     <Label Class="col-sm-3" For="confirm" HelpText="If you are changing your password you must enter it again to confirm it matches" ResourceKey="Confirm"></Label> | ||||
|                     <div class="col-sm-9"> | ||||
|                         <input id="confirm" type="password" class="form-control" @bind="@_confirm" autocomplete="new-password" /> | ||||
|                     </div> | ||||
|                 </div> | ||||
|                 <div class="row mb-1 align-items-center"> | ||||
|                     <Label Class="col-sm-3" For="email" HelpText="Your email address where you wish to receive notifications" ResourceKey="Email"></Label> | ||||
|                     <div class="col-sm-9"> | ||||
|                         <input id="email" class="form-control" @bind="@_email" /> | ||||
|                     </div> | ||||
|                 </div> | ||||
|                 <div class="row mb-1 align-items-center"> | ||||
|                     <Label Class="col-sm-3" For="displayname" HelpText="Your full name" ResourceKey="DisplayName"></Label> | ||||
|                     <div class="col-sm-9"> | ||||
|                         <input id="displayname" class="form-control" @bind="@_displayname" /> | ||||
|                     </div> | ||||
|                 </div> | ||||
|             </div> | ||||
|             <br /> | ||||
|             <button type="button" class="btn btn-primary" @onclick="Register">@Localizer["Register"]</button> | ||||
|             <button type="button" class="btn btn-secondary" @onclick="Cancel">@SharedLocalizer["Cancel"]</button> | ||||
|         </NotAuthorized> | ||||
|     </AuthorizeView> | ||||
|                 <br /> | ||||
|                 <button type="button" class="btn btn-primary" @onclick="Register">@Localizer["Register"]</button> | ||||
|                 <button type="button" class="btn btn-secondary" @onclick="Cancel">@SharedLocalizer["Cancel"]</button> | ||||
|             </NotAuthorized> | ||||
|         </AuthorizeView> | ||||
|     </form> | ||||
| } | ||||
| else | ||||
| { | ||||
| @ -61,6 +63,8 @@ else | ||||
|  | ||||
| @code { | ||||
|     private string _username = string.Empty; | ||||
|     private ElementReference form; | ||||
|     private bool validated = false; | ||||
|     private string _password = string.Empty; | ||||
|     private string _confirm = string.Empty; | ||||
|     private string _email = string.Empty; | ||||
| @ -70,49 +74,58 @@ else | ||||
|  | ||||
|     private async Task Register() | ||||
|     { | ||||
|         try | ||||
|         validated = true; | ||||
|         var interop = new Interop(JSRuntime); | ||||
|         if (await interop.FormValid(form)) | ||||
|         { | ||||
|             bool _isEmailValid = Utilities.IsValidEmail(_email); | ||||
|  | ||||
|             if (_username != "" && _password != "" && _confirm != "" && _isEmailValid) | ||||
|             try | ||||
|             { | ||||
|                 if (_password == _confirm) | ||||
|                 { | ||||
|                     var user = new User | ||||
|                     { | ||||
|                         SiteId = PageState.Site.SiteId, | ||||
|                         Username = _username, | ||||
|                         DisplayName = (_displayname == string.Empty ? _username : _displayname), | ||||
|                         Email = _email, | ||||
|                         Password = _password | ||||
|                     }; | ||||
|                     user = await UserService.AddUserAsync(user); | ||||
|                 bool _isEmailValid = Utilities.IsValidEmail(_email); | ||||
|  | ||||
|                     if (user != null) | ||||
|                 if (_username != "" && _password != "" && _confirm != "" && _isEmailValid) | ||||
|                 { | ||||
|                     if (_password == _confirm) | ||||
|                     { | ||||
|                         await logger.LogInformation("User Created {Username} {Email}", _username, _email); | ||||
|                         AddModuleMessage(Localizer["Info.User.AccountCreate"], MessageType.Info); | ||||
|                         var user = new User | ||||
|                         { | ||||
|                             SiteId = PageState.Site.SiteId, | ||||
|                             Username = _username, | ||||
|                             DisplayName = (_displayname == string.Empty ? _username : _displayname), | ||||
|                             Email = _email, | ||||
|                             Password = _password | ||||
|                         }; | ||||
|                         user = await UserService.AddUserAsync(user); | ||||
|  | ||||
|                         if (user != null) | ||||
|                         { | ||||
|                             await logger.LogInformation("User Created {Username} {Email}", _username, _email); | ||||
|                             AddModuleMessage(Localizer["Info.User.AccountCreate"], MessageType.Info); | ||||
|                         } | ||||
|                         else | ||||
|                         { | ||||
|                             await logger.LogError("Error Adding User {Username} {Email}", _username, _email); | ||||
|                             AddModuleMessage(Localizer["Error.User.AddInfo"], MessageType.Error); | ||||
|                         } | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         await logger.LogError("Error Adding User {Username} {Email}", _username, _email); | ||||
|                         AddModuleMessage(Localizer["Error.User.AddInfo"], MessageType.Error); | ||||
|                         AddModuleMessage(Localizer["Message.Password.NoMatch"], MessageType.Warning); | ||||
|                     } | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     AddModuleMessage(Localizer["Message.Password.NoMatch"], MessageType.Warning); | ||||
|                     AddModuleMessage(Localizer["Message.Required.UserInfo"], MessageType.Warning); | ||||
|                 } | ||||
|             } | ||||
|             else | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 AddModuleMessage(Localizer["Message.Required.UserInfo"], MessageType.Warning); | ||||
|                 await logger.LogError(ex, "Error Adding User {Username} {Email} {Error}", _username, _email, ex.Message); | ||||
|                 AddModuleMessage(Localizer["Error.User.Add"], MessageType.Error); | ||||
|             } | ||||
|         } | ||||
|         catch (Exception ex) | ||||
|         else | ||||
|         { | ||||
|             await logger.LogError(ex, "Error Adding User {Username} {Email} {Error}", _username, _email, ex.Message); | ||||
|             AddModuleMessage(Localizer["Error.User.Add"], MessageType.Error); | ||||
|             AddModuleMessage(SharedLocalizer["Message.InfoRequired"], MessageType.Warning); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -120,4 +133,4 @@ else | ||||
|     { | ||||
|         NavigationManager.NavigateTo(NavigateUrl(string.Empty)); | ||||
|     } | ||||
| } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Grayson Walker
					Grayson Walker