fix: started to fix the register and email confirm process

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-06-12 23:17:29 +02:00
parent 5282054cae
commit 2fa9ca826e
3 changed files with 58 additions and 27 deletions

View file

@ -1,49 +1,61 @@
@page "/sys/click/confirmmail/{userid}/{email}/{token}" @page "/sys/click/confirmmail/{userid}/{oldmail}/{proposedemail}/{token}"
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Data.Repository @using ImageBoardServerApp.Data.Repository
<h3>Confirm your Email</h3> <h3>Confirm your Email</h3>
<span>@msg</span> <span>Confirmed email. Check Account Settings.</span>
@code { @code {
private string msg { get; set; } = "Loading..."; private string msg { get; set; }
[Parameter] [Parameter]
[Required] [Required]
public string userid { get; set; } public string userid { get; set; }
[Parameter] [Parameter]
[Required] [Required]
public string email { get; set; } public string proposedemail { get; set; }
[Parameter]
[Required]
public string oldmail { get; set; }
[Parameter] [Parameter]
[Required] [Required]
public string token { get; set; } public string token { get; set; }
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
protected override async Task OnInitializedAsync()
{
if (!int.TryParse(userid, out _)) if (!int.TryParse(userid, out _))
{ {
msg = "malformed userid."; msg = "malformed userid.";
return; return;
} }
var user = await UsersRepository.getUserByIdAsync(int.Parse(userid)); var user = await UsersRepository.getUserByIdAsync(int.Parse(userid));
if (user.ConfirmedEmail)
return;
if (user == null) if (user == null)
{ {
msg = "Could not find user."; msg = "Could not find user.";
return; return;
} }
if (user.Email != email) if (user.Email != oldmail)
{ {
msg = "This email is not specified to this account."; msg = "This email is not specified to this account.";
return; return;
} }
if (user.ProposedEmail != proposedemail)
{
msg = "This is not the specified new mail to this account.";
return;
}
if (user.ConfirmEmailToken != token) if (user.ConfirmEmailToken != token)
{ {
msg = "This token is not associated with the specified account."; msg = "This token is not associated with the specified account.";

View file

@ -61,9 +61,11 @@
return; return;
} }
Console.WriteLine("Registering..."); Console.WriteLine("Registering...");
var hash = TheManager.getmd5Hash();
UserData userToCreate = new UserData() UserData userToCreate = new UserData()
{ {
Email = Email, Email = Email,
ProposedEmail = Email,
Password = BCrypt.Net.BCrypt.HashPassword(Password), Password = BCrypt.Net.BCrypt.HashPassword(Password),
Role = "User", Role = "User",
TimeBanned = -1, TimeBanned = -1,
@ -71,7 +73,7 @@
BanReason = "Not banned", BanReason = "Not banned",
ConfirmedEmail = false, ConfirmedEmail = false,
ResetPasswordExpiresAt = -1, ResetPasswordExpiresAt = -1,
ConfirmEmailToken = TheManager.getmd5Hash(), ConfirmEmailToken = hash,
ResetPasswordToken = "-1" ResetPasswordToken = "-1"
}; };
if (await UsersRepository.getUserByEmailAsync(Email) != null) if (await UsersRepository.getUserByEmailAsync(Email) != null)
@ -80,9 +82,12 @@
return; 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); var user = await UsersRepository.getUserByEmailRawAsync(Email);
if (user == null) if (user == null)

View file

@ -1,6 +1,7 @@
@page "/sys/you" @page "/sys/you"
@using ImageBoardServerApp.Auth @using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository @using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Util
@inject AuthenticationStateProvider authStateProvider @inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager @inject NavigationManager navManager
@ -12,6 +13,11 @@
<br/> <br/>
<span>Email: @userid</span> <span>Email: @userid</span>
<br/> <br/>
@if (isMailConfirmedMsg != null)
{
<span>@isMailConfirmedMsg</span>
<br/>
}
<form> <form>
<RadzenFormField Text="Enter new email address" Variant="Variant.Outlined"> <RadzenFormField Text="Enter new email address" Variant="Variant.Outlined">
<RadzenTextBox @bind-Value="@newMail"/> <RadzenTextBox @bind-Value="@newMail"/>
@ -45,10 +51,9 @@
private string userid { get; set; } = ""; private string userid { get; set; } = "";
private string newMail { 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() protected override async Task OnInitializedAsync()
{ {
@ -58,10 +63,14 @@
{ {
userid = user.User.Identity.Name; 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() private async Task changeEmail()
{ {
msg = "Checking...";
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider; var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync(); var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User; var usr = user.User;
@ -71,6 +80,7 @@
msg = "Could not find user."; msg = "Could not find user.";
return; return;
} }
if (newMail == null || newMail == "" || !newMail.Contains("@") || !newMail.Contains(".")) if (newMail == null || newMail == "" || !newMail.Contains("@") || !newMail.Contains("."))
{ {
msg = "The new email is not valid."; msg = "The new email is not valid.";
@ -82,20 +92,24 @@
msg = "This email is already in use."; msg = "This email is already in use.";
return; return;
} }
msg = "Generating...";
var hash = TheManager.getmd5Hash();
foundusr.Email = newMail; foundusr.Email = newMail;
foundusr.ConfirmEmailToken = hash;
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
foundusr.ConfirmedEmail = false; foundusr.ConfirmedEmail = false;
await UsersRepository.updateUserAsync(foundusr); await UsersRepository.updateUserAsync(foundusr);
}
protected override async Task OnAfterRenderAsync(bool firstRender) Postman.sendMail(newMail,
{ "Confirm email",
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider; "Confirm you email:\n" +
var user = await cauthStateProvder.GetAuthenticationStateAsync(); $"https://bulletboards.xyz/sys/click/confirmmail/{foundusr.UserID}/{foundusr.Email}/{newMail}/{hash}");
var usr = user.User;
u = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name); var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvider;
isMailConfirmedMsg = u.ConfirmedEmail ? "Email is confirmed" : "Email is NOT confirmed"; await customAuthStateProvider.UpdateAuthenticationStateAsync(foundusr);
await base.OnAfterRenderAsync(firstRender); navManager.NavigateTo("/sys/you", true, true);
} }
} }