bulletboards/ImageBoardServerApp/Pages/Accounts/UserPage.razor

91 lines
3.1 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
2023-02-12 18:06:33 +00:00
<AuthorizeView>
<Authorized>
<h3>YOU</h3>
<br/>
<span>Email: @mail</span>
<br/>
<span>Email: </span>
<input type="email" id="email" @bind="newMail" />
<a @onclick="changeEmail" href="javascript:void(0)">[Change Email]</a>
<br/>
<span>Password: </span>
<input type="password" id="password" @bind="newPassword" />
<a @onclick="changePassword" href="javascript:void(0)">[Change Password]</a>
<br/>
<a @onclick="logout" href="javascript:void(0)">[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 {
2023-02-12 18:06:33 +00:00
private string mail { get; set; } = "";
private string newMail { get; set; }
private string newPassword { get; set; }
protected override async Task OnInitializedAsync()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
if (user.User.Identity.IsAuthenticated)
{
mail = user.User.Identity.Name;
}
}
private async Task logout()
{
var customAuthStateProvider = (CustomAuthenticationStateProvider) authStateProvider;
await customAuthStateProvider.UpdateAuthenticationStateAsync(null);
navManager.NavigateTo("/", true);
}
private async Task changeEmail()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
if (foundusr == null)
{
return;
}
int userID = foundusr.UserID;
if (newMail == null || newMail == "" || !newMail.Contains("@"))
{
return;
}
foundusr.Email = newMail;
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr);
}
private async Task changePassword()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
if (foundusr == null)
{
return;
}
int userID = foundusr.UserID;
if (newPassword == null || newPassword == "")
{
return;
}
foundusr.Password = BCrypt.Net.BCrypt.HashPassword(newPassword);
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
await UsersRepository.updateUserAsync(foundusr);
}
}