bulletboards/ImageBoardServerApp/Pages/Accounts/UserPage.razor

101 lines
3.3 KiB
Text
Raw Normal View History

@page "/sys/you"
2023-02-12 18:06:33 +00:00
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository
@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/>
<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>
2023-02-12 18:06:33 +00:00
<br/>
<br/>
<a href="/sys/logout">[Logout]</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 UserData u { 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
}
}
2023-02-12 18:06:33 +00:00
private async Task changeEmail()
{
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;
}
2023-02-12 18:06:33 +00:00
foundusr.Email = newMail;
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
foundusr.ConfirmedEmail = false;
2023-02-12 18:06:33 +00:00
await UsersRepository.updateUserAsync(foundusr);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
u = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name);
isMailConfirmedMsg = u.ConfirmedEmail ? "Email is confirmed" : "Email is NOT confirmed";
await base.OnAfterRenderAsync(firstRender);
}
}