bulletboards/ImageBoardServerApp/Pages/Accounts/UserPage.razor
limited_dev 2fa9ca826e fix: started to fix the register and email confirm process
Signed-off-by: limited_dev <loginakkisativ@gmail.com>
2023-06-12 23:17:29 +02:00

115 lines
No EOL
3.7 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"/>
<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>
</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 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.Email = newMail;
foundusr.ConfirmEmailToken = hash;
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
foundusr.ConfirmedEmail = false;
await UsersRepository.updateUserAsync(foundusr);
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);
}
}