From 059c755d141d2cae803c68cc77d9f87f28ffc7eb Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Thu, 19 Jun 2025 16:49:43 -0700 Subject: [PATCH] Update client to use forms --- .../ActivityPages/ForgotPassword.razor | 21 +++-- .../Pages/Account/ActivityPages/Login.razor | 54 +++++++------ .../Account/ActivityPages/Register.razor | 80 +++++++------------ .../Account/ActivityPages/ResetPassword.razor | 38 +++++---- .../Account/ActivityPages/VerifyEmail.razor | 6 +- .../Pages/Account/Manage/Account.razor | 31 ++++--- .../Pages/Account/Manage/WebsiteData.razor | 12 +-- .../Pages/Projects/Downloads.razor | 19 +---- 8 files changed, 126 insertions(+), 135 deletions(-) diff --git a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ForgotPassword.razor b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ForgotPassword.razor index fefc28c..ae456a6 100755 --- a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ForgotPassword.razor +++ b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ForgotPassword.razor @@ -34,10 +34,21 @@ } public async Task TrySendCode() { - Result = "Waiting on response from server"; - HttpResponseMessage TestLogin = await Http.PostAsJsonAsync("api/account/sendresetpassword", new Account(){ Email = Email }); - Result = await TestLogin.Content.ReadAsStringAsync(); - + MailAddress addr = new MailAddress(Email); + if ( addr.Address != Email ){ + Result = "Enter a valid email address"; + }else{ + Result = ""; + } + if (string.IsNullOrEmpty(Result)){ + Dictionary formData = new Dictionary{ + { "Email", Email } + }; + Result = "Waiting on response from server"; + HttpResponseMessage TestLogin = await Http.PostAsync("api/account/sendresetpassword", new FormUrlEncodedContent(formData) ); + Result = await TestLogin.Content.ReadAsStringAsync(); + } + base.StateHasChanged(); } -} +} \ No newline at end of file diff --git a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Login.razor b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Login.razor index 9d32bb4..9ac79f9 100755 --- a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Login.razor +++ b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Login.razor @@ -54,34 +54,36 @@ Loading = "Waiting for login response from server"; ReturnURL = string.IsNullOrEmpty(ReturnURL) ? "/" : ReturnURL; ErrorMsgs = new List(); - - if( !string.IsNullOrEmpty(UserName) ) { - if( !string.IsNullOrEmpty(Password) ) { - if (Password.Length >= 6 ) { - HttpResponseMessage TestLogin = await Http.PostAsJsonAsync("api/account/login", new MistoxWebsite.Shared.Account(){ UserName = UserName, PasswordHash = Password, EmailVerified = StayLoggedIn }); - string result = await TestLogin.Content.ReadAsStringAsync(); - Account? user = JsonConvert.DeserializeObject(result); - if (user == null ) { - ErrorMsgs.Add("No response from the server"); - base.StateHasChanged(); - return; - } - if ( string.IsNullOrEmpty(user.Error) ) { - ErrorMsgs.Add("Login Success"); - Nav.NavigateTo("/", true); - } else { - ErrorMsgs.Add(user.Error); - } - Loading = ""; - } else { - ErrorMsgs.Add("Password must be at least 6 Characters long"); - } - } else { - ErrorMsgs.Add("The 'password' field is required"); - } - } else{ + if( string.IsNullOrEmpty(UserName) ) { ErrorMsgs.Add("The 'username' field is required"); } + if( string.IsNullOrEmpty(Password) ) { + ErrorMsgs.Add("The 'password' field is required"); + } + if (Password.Length < 6 ) { + ErrorMsgs.Add("Password must be at least 6 Characters long"); + } + if (ErrorMsgs.Count == 0){ + Dictionary formData = new Dictionary{ + { "UserName", UserName }, + { "PasswordHash", Password }, + { "StayLoggedIn", StayLoggedIn.ToString() } + }; + HttpResponseMessage TestLogin = await Http.PostAsync("api/account/login", new FormUrlEncodedContent(formData) ); + string result = await TestLogin.Content.ReadAsStringAsync(); + Account? user = JsonConvert.DeserializeObject(result); + if (user == null ) { + ErrorMsgs.Add("No response from the server"); + base.StateHasChanged(); + return; + } + if ( string.IsNullOrEmpty(user.Error) ) { + ErrorMsgs.Add("Login Success"); + Nav.NavigateTo(ReturnURL, true); + } else { + ErrorMsgs.Add(user.Error); + } + } Loading = ""; base.StateHasChanged(); } diff --git a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Register.razor b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Register.razor index 48dc4bd..681f754 100755 --- a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Register.razor +++ b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/Register.razor @@ -52,63 +52,41 @@ } } - public bool CheckEmail(string email ) { - int ATcount = 0; - int DOTcount = 0; - char[] cmail = email.ToArray(); - foreach(char cur in cmail ) { - if(cur == '@' ) { - ATcount += 1; - }else if(cur == '.' ) { - DOTcount += 1; - } - } - if (ATcount == 1 && DOTcount >= 1 ) { - return true; - } - return false; - } - public async Task TryRegister() { Loading = "Waiting for a response from the server"; ReturnURL = string.IsNullOrEmpty(ReturnURL) ? "/" : ReturnURL; - ErrorMsgs = new List(); - - if ( !string.IsNullOrEmpty(Email) ){ - if( CheckEmail( Email ) ) { - if( !string.IsNullOrEmpty(UserName) ) { - if( !string.IsNullOrEmpty(Password) ) { - if (Password.Length >= 6 ) { - HttpResponseMessage TestRegister = await Http.PostAsJsonAsync("api/account/register", new Account(){ - UserName = UserName, - Email = Email, - PasswordHash = Password, - EmailVerified = false, - }); - Account? user = await TestRegister.Content.ReadFromJsonAsync(); - if ( string.IsNullOrEmpty(user?.Error) ) { - ErrorMsgs.Add("Register Success"); - Nav.NavigateTo("/", true); - } else { - ErrorMsgs.Add( user.Error ); - } - Loading = ""; - }else{ - ErrorMsgs.Add("Password must be at least 6 Characters long"); - } - }else{ - ErrorMsgs.Add("The 'password' field is required"); - } - }else{ - ErrorMsgs.Add("The 'username' field is required"); - } - }else{ - ErrorMsgs.Add("Please check your email address"); - } - }else{ + MailAddress addr = new MailAddress(Email); + if ( addr.Address != Email ){ + ErrorMsgs.Add("Please check your email address"); + } + if (Password.Length < 6 ) { + ErrorMsgs.Add("Password must be at least 6 Characters long"); + } + if( string.IsNullOrEmpty(UserName) ) { + ErrorMsgs.Add("The 'username' field is required"); + } + if( string.IsNullOrEmpty(Password) ) { + ErrorMsgs.Add("The 'password' field is required"); + } + if ( string.IsNullOrEmpty(Email) ){ ErrorMsgs.Add("The 'email' field is required"); } + if (ErrorMsgs.Count == 0){ + Dictionary formData = new Dictionary{ + { "Email", Email }, + { "UserName", UserName }, + { "PasswordHash", Password }, + }; + HttpResponseMessage TestRegister = await Http.PostAsync("api/account/register", new FormUrlEncodedContent(formData) ); + Account? user = await TestRegister.Content.ReadFromJsonAsync(); + if ( string.IsNullOrEmpty(user?.Error) ) { + ErrorMsgs.Add("Register Success"); + Nav.NavigateTo(ReturnURL, true); + } else { + ErrorMsgs.Add( user.Error ); + } + } Loading = ""; base.StateHasChanged(); } diff --git a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ResetPassword.razor b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ResetPassword.razor index 041e586..2a48f8b 100755 --- a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ResetPassword.razor +++ b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/ResetPassword.razor @@ -38,7 +38,6 @@ [Parameter] [SupplyParameterFromQuery] public string ResetPwd { get; set; } = ""; - public string NewPassword{ get; set; } = ""; public string RepeatPassword{ get; set; } = ""; @@ -52,24 +51,29 @@ protected async Task TryChange() { Result = "Waiting on response from server"; - if (NewPassword.Length >= 6){ - if (NewPassword == RepeatPassword){ - HttpResponseMessage TestLogin = await Http.PostAsJsonAsync("api/account/resetpassword", new Account(){ UserName = UserName, PasswordHash = NewPassword, Error = ResetPwd }); - string result = await TestLogin.Content.ReadAsStringAsync(); - bool success = result == "true" ? true : false; - if (success){ - Result = "Password changed successfully"; - Thread.Sleep(2000); - Nav.NavigateTo("/", true); - }else{ - Result = "Something is wrong"; - } - }else{ - Result = "Passwords must match"; - } - }else{ + if (NewPassword != RepeatPassword){ + Result = "Passwords must match"; + } + if (NewPassword.Length < 6){ Result = "Password must be at least 6 Characters long"; } + if (string.IsNullOrEmpty(Result)){ + Dictionary formData = new Dictionary{ + { "UserName", UserName }, + { "NewPassword", NewPassword }, + { "ResetToken", ResetPwd }, + }; + HttpResponseMessage TestLogin = await Http.PostAsync("api/account/resetpassword", new FormUrlEncodedContent(formData) ); + string result = await TestLogin.Content.ReadAsStringAsync(); + bool success = result == "true" ? true : false; + if (success){ + Result = "Password changed successfully"; + Thread.Sleep(2000); + Nav.NavigateTo("/", true); + }else{ + Result = "Something is wrong"; + } + } } } diff --git a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/VerifyEmail.razor b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/VerifyEmail.razor index 4011561..266475c 100755 --- a/src/MistoxWebsite.Client/Pages/Account/ActivityPages/VerifyEmail.razor +++ b/src/MistoxWebsite.Client/Pages/Account/ActivityPages/VerifyEmail.razor @@ -26,7 +26,11 @@ public string Result{ get; set; } = ""; protected override async Task OnInitializedAsync() { - HttpResponseMessage Query = await Http.PostAsJsonAsync("api/account/verifyemail", new Account(){ UserName = UserName, PasswordHash = Guid }); + Dictionary formData = new Dictionary{ + { "UserName", UserName }, + { "EmailToken", Guid }, + }; + HttpResponseMessage Query = await Http.PostAsync("api/account/verifyemail", new FormUrlEncodedContent(formData) ); bool Answer = await Query.Content.ReadFromJsonAsync(); if (Answer == true ) { Result = "Verified Email Successfully"; diff --git a/src/MistoxWebsite.Client/Pages/Account/Manage/Account.razor b/src/MistoxWebsite.Client/Pages/Account/Manage/Account.razor index 8f2c0ee..9eca19d 100755 --- a/src/MistoxWebsite.Client/Pages/Account/Manage/Account.razor +++ b/src/MistoxWebsite.Client/Pages/Account/Manage/Account.razor @@ -41,7 +41,7 @@ @code { - public MistoxWebsite.Shared.Account? _account = null; + public MistoxWebsite.Shared.Database.Account? _account = null; public int MaxFailedLogin = 0; public bool FailedLoginToggle = false; @@ -49,18 +49,19 @@ public async Task SubmitLoginLock() { if (_account != null ) { - _account.SiteData.FailedPasswordLock = FailedLoginToggle; - _account.SiteData.PasswordAttempts = MaxFailedLogin; - _account.PasswordHash = ""; - HttpResponseMessage SendVerifyEmail = await Http.PostAsJsonAsync("api/account/toggleAccountLock", _account); - LoginCounterResult = await SendVerifyEmail.Content.ReadAsStringAsync(); + Dictionary formData = new Dictionary{ + { "UserName", _account.UserName }, + { "AccountLock", FailedLoginToggle.ToString() }, + }; + HttpResponseMessage Query = await Http.PostAsync("api/account/toggleAccountLock", new FormUrlEncodedContent(formData) ); + LoginCounterResult = await Query.Content.ReadAsStringAsync(); } } protected override async Task OnInitializedAsync() { HttpResponseMessage x = await Http.PostAsync("api/account/get", new StringContent("")); string body = await x.Content.ReadAsStringAsync(); - _account = JsonConvert.DeserializeObject(body); + _account = JsonConvert.DeserializeObject(body); if (_account != null){ FailedLoginToggle = _account.SiteData.FailedPasswordLock; MaxFailedLogin = _account.SiteData.PasswordAttempts; @@ -73,8 +74,11 @@ public async Task SendVerifyEmail() { if (_account != null){ - HttpResponseMessage SendVerifyEmail = await Http.PostAsJsonAsync("api/account/sendverifyemail", new MistoxWebsite.Shared.Account(){ UserName = _account.UserName }); - bool result = await SendVerifyEmail.Content.ReadFromJsonAsync(); + Dictionary formData = new Dictionary{ + { "UserName", _account.UserName }, + }; + HttpResponseMessage Query = await Http.PostAsync("api/account/sendverifyemail", new FormUrlEncodedContent(formData) ); + bool result = await Query.Content.ReadFromJsonAsync(); if (result == true ) { EmailSentResult = "Email Sent"; } else { @@ -108,8 +112,13 @@ return; } if (_account != null){ - HttpResponseMessage TryChangePassword = await Http.PostAsJsonAsync("api/account/changepassword", new MistoxWebsite.Shared.Account(){ UserName = _account.UserName, PasswordHash = CurPass, Error = NewPass1 }); - bool resultText = await TryChangePassword.Content.ReadFromJsonAsync(); + Dictionary formData = new Dictionary{ + { "UserName", _account.UserName }, + { "OldPassword", CurPass }, + { "NewPassword", NewPass1 } + }; + HttpResponseMessage Query = await Http.PostAsync("api/account/changepassword", new FormUrlEncodedContent(formData) ); + bool resultText = await Query.Content.ReadFromJsonAsync(); if (resultText == true ) { PasswordErrorText = "Password changed successfully"; } else { diff --git a/src/MistoxWebsite.Client/Pages/Account/Manage/WebsiteData.razor b/src/MistoxWebsite.Client/Pages/Account/Manage/WebsiteData.razor index 310cbfe..8ff246e 100755 --- a/src/MistoxWebsite.Client/Pages/Account/Manage/WebsiteData.razor +++ b/src/MistoxWebsite.Client/Pages/Account/Manage/WebsiteData.razor @@ -119,12 +119,12 @@ } async Task confirmDeleteAccount() { - HttpResponseMessage Delete = await Http.PostAsJsonAsync( "api/account/delete", new MistoxWebsite.Shared.Account(){ - ID = Statics.User.ID, - UserName = Statics.User.Email, - PasswordHash = Password - }); - string result = await Delete.Content.ReadAsStringAsync(); + Dictionary formData = new Dictionary{ + { "UserName", Statics.User.UserName }, + { "Password", Password }, + }; + HttpResponseMessage Query = await Http.PostAsync("api/account/delete", new FormUrlEncodedContent(formData) ); + string result = await Query.Content.ReadAsStringAsync(); bool status = result == "true" ? true : false; if (status){ await Http.PostAsync("api/account/logout", new StringContent("")); diff --git a/src/MistoxWebsite.Client/Pages/Projects/Downloads.razor b/src/MistoxWebsite.Client/Pages/Projects/Downloads.razor index b5322a1..c642617 100755 --- a/src/MistoxWebsite.Client/Pages/Projects/Downloads.razor +++ b/src/MistoxWebsite.Client/Pages/Projects/Downloads.razor @@ -6,28 +6,11 @@
- @if (output != null ) { - - } +
- @ErrorTxt
@code{ - public string ErrorTxt = ""; - public DirObj? output = null; - - protected override async void OnInitialized() { - try { - byte[] resultBody = await (await Http.PostAsync( "api/product/showdownloads", new StringContent("") )).Content.ReadAsByteArrayAsync(); - string JsonData = Encoding.UTF8.GetString(resultBody); - output = JsonConvert.DeserializeObject( JsonData ); - base.StateHasChanged(); - } catch( Exception e ) { - ErrorTxt = "Error : " + e.ToString(); - } - } - } \ No newline at end of file