bulletboards/ImageBoardServerApp/Pages/Accounts/UserPage.razor

159 lines
5.2 KiB
Text
Raw Permalink Normal View History

@page "/sys/you"
2023-02-12 18:06:33 +00:00
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Util
2023-02-12 18:06:33 +00:00
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager
<PageTitle>YOU - Bulletboards</PageTitle>
2023-02-12 18:06:33 +00:00
<AuthorizeView>
<Authorized>
<h3>YOUR ACCOUNT</h3>
2023-02-12 18:06:33 +00:00
<br/>
<span>Email: @userid</span>
2023-02-12 18:06:33 +00:00
<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>
2023-02-12 18:06:33 +00:00
<br/>
<br/>
<a href="/sys/logout">[Logout]</a>
<br/>
<br/>
<a href="/sys/delacc">[Delete Account]</a>
2023-02-12 18:06:33 +00:00
</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; } = "";
2023-02-12 18:06:33 +00:00
private string newMail { get; set; }
private string msg { get; set; } = "";
private string isMailConfirmedMsg { get; set; } = "";
2023-02-12 18:06:33 +00:00
protected override async Task OnInitializedAsync()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
if (user.User.Identity.IsAuthenticated)
{
userid = user.User.Identity.Name;
2023-02-12 18:06:33 +00:00
}
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(user.User.Identity.Name);
if (foundusr != null)
isMailConfirmedMsg = foundusr.ConfirmedEmail ? "Email is confirmed" : "Email is NOT confirmed";
2023-02-12 18:06:33 +00:00
}
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";
}
2023-02-12 18:06:33 +00:00
private async Task changeEmail()
{
msg = "Checking...";
2023-02-12 18:06:33 +00:00
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name);
2023-02-12 18:06:33 +00:00
if (foundusr == null)
{
msg = "Could not find user.";
2023-02-12 18:06:33 +00:00
return;
}
if (newMail == null || newMail == "" || !newMail.Contains("@") || !newMail.Contains("."))
2023-02-12 18:06:33 +00:00
{
msg = "The new email is not valid.";
2023-02-12 18:06:33 +00:00
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;
2023-02-12 18:06:33 +00:00
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
foundusr.ConfirmedEmail = false;
2023-02-12 18:06:33 +00:00
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);
}
}