fix: fixed issues with email confirmation

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-06-13 19:59:21 +02:00
parent 3abc1ba7ab
commit 712ac3c020
4 changed files with 63 additions and 5 deletions

View file

@ -1,6 +1,7 @@
@page "/sys/click/confirmmail/{userid}/{oldmail}/{proposedemail}/{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
@inject NavigationManager navManager
<h3>Confirm your Email</h3> <h3>Confirm your Email</h3>
<span>Confirmed email. Check Account Settings.</span> <span>Confirmed email. Check Account Settings.</span>
@ -62,12 +63,17 @@
return; return;
} }
Console.WriteLine("");
user.ConfirmEmailToken = "-1"; user.ConfirmEmailToken = "-1";
user.ConfirmedEmail = true; user.ConfirmedEmail = true;
user.Email = proposedemail;
await UsersRepository.updateUserAsync(user); await UsersRepository.updateUserAsync(user);
msg = "The email has been confirmed."; msg = "The email has been confirmed.";
navManager.NavigateTo("/sys/click/red/_sys_logout");
} }
} }

View file

@ -21,8 +21,8 @@
<RadzenButton Click=@login Text="login" ButtonStyle="ButtonStyle.Secondary"/> <RadzenButton Click=@login Text="login" ButtonStyle="ButtonStyle.Secondary"/>
<br/> <br/>
</form> </form>
<a href="/sys/resetpw">Reset Password</a>
</div> </div>
<a href="/sys/resetpw">Reset Password</a>
@code { @code {
private bool verified; private bool verified;

View file

@ -27,7 +27,12 @@
<br/> <br/>
<br/> <br/>
<br/> <br/>
@if (msg != null)
{
<div>
<span class="msg">@msg</span> <span class="msg">@msg</span>
</div>
}
</div> </div>
@ -42,6 +47,7 @@
private async Task login() private async Task login()
{ {
msg = "Checking...";
if (!is18) if (!is18)
{ {
msg = "You have to be atleast 18 years old to register."; msg = "You have to be atleast 18 years old to register.";
@ -58,7 +64,9 @@
return; return;
} }
Console.WriteLine("Registering..."); Console.WriteLine("Registering...");
msg = "Generating...";
var hash = TheManager.getmd5Hash(); var hash = TheManager.getmd5Hash();
msg = "Saving...";
UserData userToCreate = new UserData() UserData userToCreate = new UserData()
{ {
Email = Email, Email = Email,
@ -81,15 +89,19 @@
int uid = await UsersRepository.createUserAsync(userToCreate); int uid = await UsersRepository.createUserAsync(userToCreate);
msg = "Sending....";
Postman.sendMail(Email, Postman.sendMail(Email,
"Confirm email", "Confirm email",
"Confirm you email:\n" + "Confirm you email:\n" +
$"https://bulletboards.xyz/sys/click/confirmmail/{uid}/{Email}/{Email}/{hash}"); $"https://bulletboards.xyz/sys/click/confirmmail/{uid}/{Email}/{Email}/{hash}");
msg = "Done. Check email.";
var user = await UsersRepository.getUserByEmailRawAsync(Email); var user = await UsersRepository.getUserByEmailRawAsync(Email);
if (user == null) if (user == null)
{ {
await js.InvokeVoidAsync("alert", "User does not exist"); await js.InvokeVoidAsync("alert", "User does not exist. If this happens, please notify developer.");
return; return;
} }
verified = BCrypt.Net.BCrypt.Verify(Password, user.Password); verified = BCrypt.Net.BCrypt.Verify(Password, user.Password);

View file

@ -25,6 +25,7 @@
<br/> <br/>
<br/> <br/>
<RadzenButton Click=@changeEmail Text="Set Email" ButtonStyle="ButtonStyle.Secondary"/> <RadzenButton Click=@changeEmail Text="Set Email" ButtonStyle="ButtonStyle.Secondary"/>
<RadzenButton Click=@resendEmail Text="Resend Email" ButtonStyle="ButtonStyle.Secondary"></RadzenButton>
<br/> <br/>
</form> </form>
@if (msg != null) @if (msg != null)
@ -72,6 +73,43 @@
isMailConfirmedMsg = foundusr.ConfirmedEmail ? "Email is confirmed" : "Email is NOT confirmed"; isMailConfirmedMsg = foundusr.ConfirmedEmail ? "Email is confirmed" : "Email is NOT confirmed";
} }
private async Task resendEmail()
{
msg = "Checking...";
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name);
if (foundusr == null)
{
msg = "Could not find user.";
return;
}
UserData u2 = await UsersRepository.getUserByEmailRawAsync(newMail);
if (u2 != null)
{
msg = "This email is already in use.";
return;
}
msg = "Generating...";
var hash = TheManager.getmd5Hash();
foundusr.ConfirmEmailToken = hash;
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
foundusr.ConfirmedEmail = false;
msg = "Sending...";
Postman.sendMail(foundusr.ProposedEmail,
"Confirm email",
"Confirm you email:\n" +
$"https://bulletboards.xyz/sys/click/confirmmail/{foundusr.UserID}/{foundusr.Email}/{foundusr.ProposedEmail}/{hash}");
await UsersRepository.updateUserAsync(foundusr);
msg = "Done. Check mail";
}
private async Task changeEmail() private async Task changeEmail()
{ {
msg = "Checking..."; msg = "Checking...";
@ -100,12 +138,13 @@
msg = "Generating..."; msg = "Generating...";
var hash = TheManager.getmd5Hash(); var hash = TheManager.getmd5Hash();
foundusr.Email = newMail; foundusr.ProposedEmail = newMail;
foundusr.ConfirmEmailToken = hash; 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);
msg = "Sending...";
Postman.sendMail(newMail, Postman.sendMail(newMail,
"Confirm email", "Confirm email",
"Confirm you email:\n" + "Confirm you email:\n" +
@ -113,7 +152,8 @@
var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvider; var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvider;
await customAuthStateProvider.UpdateAuthenticationStateAsync(foundusr); await customAuthStateProvider.UpdateAuthenticationStateAsync(foundusr);
navManager.NavigateTo("/sys/you", true, true); msg = "Done. Check mail";
navManager.NavigateTo("/sys/click/red/_sys_you", true, true);
} }
} }