bulletboards/ImageBoardServerApp/Pages/Accounts/UserPage.razor
limited_dev 712ac3c020 fix: fixed issues with email confirmation
Signed-off-by: limited_dev <loginakkisativ@gmail.com>
2023-06-13 19:59:21 +02:00

159 lines
No EOL
5.2 KiB
Text

@page "/sys/you"
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Util
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager
<PageTitle>YOU - Bulletboards</PageTitle>
<AuthorizeView>
<Authorized>
<h3>YOUR ACCOUNT</h3>
<br/>
<span>Email: @userid</span>
<br/>
@if (isMailConfirmedMsg != null)
{
<span>@isMailConfirmedMsg</span>
<br/>
}
<form>
<RadzenFormField Text="Enter new email address" Variant="Variant.Outlined">
<RadzenTextBox @bind-Value="@newMail"/>
</RadzenFormField>
<br/>
<br/>
<RadzenButton Click=@changeEmail Text="Set Email" ButtonStyle="ButtonStyle.Secondary"/>
<RadzenButton Click=@resendEmail Text="Resend Email" ButtonStyle="ButtonStyle.Secondary"></RadzenButton>
<br/>
</form>
@if (msg != null)
{
<div>
<span class="msg">@msg</span>
</div>
}
<br/>
<br/>
<br/>
<a href="/sys/resetpw">[Click here to change your password]</a>
<br/>
<br/>
<a href="/sys/logout">[Logout]</a>
<br/>
<br/>
<a href="/sys/delacc">[Delete Account]</a>
</Authorized>
<NotAuthorized>
<span>You are not logged in.</span>
<a class="navbar-brand" href="/sys/login">Please login first.</a>
</NotAuthorized>
</AuthorizeView>
@code {
private string userid { get; set; } = "";
private string newMail { get; set; }
private string msg { get; set; } = "";
private string isMailConfirmedMsg { get; set; } = "";
protected override async Task OnInitializedAsync()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
if (user.User.Identity.IsAuthenticated)
{
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 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()
{
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;
}
if (newMail == null || newMail == "" || !newMail.Contains("@") || !newMail.Contains("."))
{
msg = "The new email is not valid.";
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.ProposedEmail = newMail;
foundusr.ConfirmEmailToken = hash;
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
foundusr.ConfirmedEmail = false;
await UsersRepository.updateUserAsync(foundusr);
msg = "Sending...";
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);
msg = "Done. Check mail";
navManager.NavigateTo("/sys/click/red/_sys_you", true, true);
}
}