From 2fa9ca826e99b872735a1b5446a988a4ad7f4c5a Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 12 Jun 2023 23:17:29 +0200 Subject: [PATCH] fix: started to fix the register and email confirm process Signed-off-by: limited_dev --- .../ClickOn/ClickOnConfirmEmail.razor | 36 ++++++++++++------ .../Pages/Accounts/Register.razor | 11 ++++-- .../Pages/Accounts/UserPage.razor | 38 +++++++++++++------ 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/ImageBoardServerApp/Pages/Accounts/ClickOn/ClickOnConfirmEmail.razor b/ImageBoardServerApp/Pages/Accounts/ClickOn/ClickOnConfirmEmail.razor index 0fca738..5701993 100644 --- a/ImageBoardServerApp/Pages/Accounts/ClickOn/ClickOnConfirmEmail.razor +++ b/ImageBoardServerApp/Pages/Accounts/ClickOn/ClickOnConfirmEmail.razor @@ -1,49 +1,61 @@ -@page "/sys/click/confirmmail/{userid}/{email}/{token}" +@page "/sys/click/confirmmail/{userid}/{oldmail}/{proposedemail}/{token}" @using System.ComponentModel.DataAnnotations @using ImageBoardServerApp.Data.Repository

Confirm your Email

-@msg +Confirmed email. Check Account Settings. @code { - private string msg { get; set; } = "Loading..."; - + private string msg { get; set; } + [Parameter] [Required] public string userid { get; set; } - + [Parameter] [Required] - public string email { get; set; } - + public string proposedemail { get; set; } + + [Parameter] + [Required] + public string oldmail { get; set; } + [Parameter] [Required] public string token { get; set; } - - protected override async Task OnParametersSetAsync() - { - await base.OnParametersSetAsync(); + protected override async Task OnInitializedAsync() + { if (!int.TryParse(userid, out _)) { msg = "malformed userid."; return; } + var user = await UsersRepository.getUserByIdAsync(int.Parse(userid)); + if (user.ConfirmedEmail) + return; + if (user == null) { msg = "Could not find user."; return; } - if (user.Email != email) + if (user.Email != oldmail) { msg = "This email is not specified to this account."; return; } + if (user.ProposedEmail != proposedemail) + { + msg = "This is not the specified new mail to this account."; + return; + } + if (user.ConfirmEmailToken != token) { msg = "This token is not associated with the specified account."; diff --git a/ImageBoardServerApp/Pages/Accounts/Register.razor b/ImageBoardServerApp/Pages/Accounts/Register.razor index 3d0351f..d7b9ee2 100644 --- a/ImageBoardServerApp/Pages/Accounts/Register.razor +++ b/ImageBoardServerApp/Pages/Accounts/Register.razor @@ -61,9 +61,11 @@ return; } Console.WriteLine("Registering..."); + var hash = TheManager.getmd5Hash(); UserData userToCreate = new UserData() { Email = Email, + ProposedEmail = Email, Password = BCrypt.Net.BCrypt.HashPassword(Password), Role = "User", TimeBanned = -1, @@ -71,7 +73,7 @@ BanReason = "Not banned", ConfirmedEmail = false, ResetPasswordExpiresAt = -1, - ConfirmEmailToken = TheManager.getmd5Hash(), + ConfirmEmailToken = hash, ResetPasswordToken = "-1" }; if (await UsersRepository.getUserByEmailAsync(Email) != null) @@ -80,9 +82,12 @@ return; } - await UsersRepository.createUserAsync(userToCreate); + int uid = await UsersRepository.createUserAsync(userToCreate); - Postman.sendMail(Email, "Confirm Email", ""); + Postman.sendMail(Email, + "Confirm email", + "Confirm you email:\n" + + $"https://bulletboards.xyz/sys/click/confirmmail/{uid}/{Email}/{Email}/{hash}"); var user = await UsersRepository.getUserByEmailRawAsync(Email); if (user == null) diff --git a/ImageBoardServerApp/Pages/Accounts/UserPage.razor b/ImageBoardServerApp/Pages/Accounts/UserPage.razor index 2147f78..0d3f26a 100644 --- a/ImageBoardServerApp/Pages/Accounts/UserPage.razor +++ b/ImageBoardServerApp/Pages/Accounts/UserPage.razor @@ -1,6 +1,7 @@ @page "/sys/you" @using ImageBoardServerApp.Auth @using ImageBoardServerApp.Data.Repository +@using ImageBoardServerApp.Util @inject AuthenticationStateProvider authStateProvider @inject NavigationManager navManager @@ -12,6 +13,11 @@
Email: @userid
+ @if (isMailConfirmedMsg != null) + { + @isMailConfirmedMsg +
+ }
@@ -45,10 +51,9 @@ private string userid { get; set; } = ""; private string newMail { get; set; } - private string msg { get; set; } + private string msg { get; set; } = ""; - private UserData u { get; set; } - private string isMailConfirmedMsg { get; set; } + private string isMailConfirmedMsg { get; set; } = ""; protected override async Task OnInitializedAsync() { @@ -58,10 +63,14 @@ { userid = user.User.Identity.Name; } + UserData foundusr = await UsersRepository.getUserByEmailRawAsync(user.User.Identity.Name); + if (foundusr != null) + isMailConfirmedMsg = foundusr.ConfirmedEmail ? "Email is confirmed" : "Email is NOT confirmed"; } private async Task changeEmail() { + msg = "Checking..."; var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider; var user = await cauthStateProvder.GetAuthenticationStateAsync(); var usr = user.User; @@ -71,6 +80,7 @@ msg = "Could not find user."; return; } + if (newMail == null || newMail == "" || !newMail.Contains("@") || !newMail.Contains(".")) { msg = "The new email is not valid."; @@ -82,20 +92,24 @@ msg = "This email is already in use."; return; } + + msg = "Generating..."; + var hash = TheManager.getmd5Hash(); + foundusr.Email = newMail; + foundusr.ConfirmEmailToken = hash; foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); foundusr.ConfirmedEmail = false; await UsersRepository.updateUserAsync(foundusr); - } - protected override async Task OnAfterRenderAsync(bool firstRender) - { - var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider; - var user = await cauthStateProvder.GetAuthenticationStateAsync(); - var usr = user.User; - u = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name); - isMailConfirmedMsg = u.ConfirmedEmail ? "Email is confirmed" : "Email is NOT confirmed"; - await base.OnAfterRenderAsync(firstRender); + Postman.sendMail(newMail, + "Confirm email", + "Confirm you email:\n" + + $"https://bulletboards.xyz/sys/click/confirmmail/{foundusr.UserID}/{foundusr.Email}/{newMail}/{hash}"); + + var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvider; + await customAuthStateProvider.UpdateAuthenticationStateAsync(foundusr); + navManager.NavigateTo("/sys/you", true, true); } } \ No newline at end of file