feat: started working on password reset and email confirmation, added columns to DB

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-06-08 23:34:59 +02:00
parent e6b2c22bab
commit 828f784fc8
13 changed files with 179 additions and 13 deletions

View file

@ -14,6 +14,9 @@ public class UserData
[Required] [Required]
public long TimeBanned { get; set; } public long TimeBanned { get; set; }
[Required]
public string BanReason { get; set; }
[Required] [Required]
public long lastActionTimeStamp { get; set; } public long lastActionTimeStamp { get; set; }
@ -35,4 +38,13 @@ public class UserData
public List<ReportData> RecivedReports { get; set; } public List<ReportData> RecivedReports { get; set; }
public string LastUsedName { get; set; } public string LastUsedName { get; set; }
public bool ConfirmedEmail { get; set; }
public string ConfirmEmailToken { get; set; }
public string ResetPasswordToken { get; set; }
public long ResetPasswordExpiresAt { get; set; }
} }

View file

@ -1,6 +1,8 @@
@page "/sys/banned" @page "/sys/banned"
<h3>Banned</h3> <h3>Banned</h3>
<PageTitle>You are BANNED!</PageTitle>
<img class="image" src="img/static/banned/@i"/> <img class="image" src="img/static/banned/@i"/>
@code { @code {

View file

@ -0,0 +1,6 @@
@page "/sys/click/confirmmail/{userid}/{email}/{token}"
<h3>Confirming your Email...</h3>
@code {
}

View file

@ -0,0 +1,51 @@
@page "/sys/click/resetpw/{userid}/{hash}"
@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Data.Repository
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager
<PageTitle>Password reset</PageTitle>
<span>Password reset for account id #@userid</span>
<div class="login">
<form>
<RadzenFormField Text="New Password" Variant="Variant.Outlined">
<RadzenPassword @bind-Value="@Password" />
</RadzenFormField>
<br/>
<br/>
<RadzenButton Click=@reset Text="reset" ButtonStyle="ButtonStyle.Secondary" />
<br/>
</form>
</div>
@code {
private string Password { get; set; }
[Parameter]
[Required]
public string userid { get; set; }
[Parameter]
[Required]
public string hash { get; set; }
public async void reset()
{
if (int.TryParse(userid, out _))
return;
var user = await UsersRepository.getUserByIdAsync(int.Parse(userid));
if (user == null)
return;
if (user.ResetPasswordToken != hash)
{
return;
}
Console.WriteLine("Resetting a password...");
user.Password = Password = BCrypt.Net.BCrypt.HashPassword(Password);
await UsersRepository.updateUserAsync(user);
}
}

View file

@ -5,8 +5,8 @@
@inject AuthenticationStateProvider authStateProvider @inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager @inject NavigationManager navManager
<PageTitle>Login - Bulletboards</PageTitle>
<h3 class="headLogin">Login to bulletbroards</h3> <h3 class="headLogin">Login to Bulletboards</h3>
<div class="login"> <div class="login">
<form> <form>
<RadzenFormField Text="Email" Variant="Variant.Outlined"> <RadzenFormField Text="Email" Variant="Variant.Outlined">
@ -19,8 +19,10 @@
<br/> <br/>
<br/> <br/>
<RadzenButton Click=@login Text="login" ButtonStyle="ButtonStyle.Secondary" /> <RadzenButton Click=@login Text="login" ButtonStyle="ButtonStyle.Secondary" />
<br/>
</form> </form>
</div> </div>
<a href="/sys/resetpw">Reset Password</a>
@code { @code {
private Variant vari = Variant.Outlined; private Variant vari = Variant.Outlined;

View file

@ -2,10 +2,13 @@
text-align: center; text-align: center;
-webkit-text-size-adjust: auto; -webkit-text-size-adjust: auto;
} }
.login{ .login{
text-align: center; text-align: center;
align-items:center; align-items:center;
font-style: unset;} font-style: unset;
}
.gaping{ .gaping{
alignment: left; alignment: left;
} }

View file

@ -0,0 +1,20 @@
@page "/sys/logout"
@using ImageBoardServerApp.Auth
@inject NavigationManager navManager
@inject AuthenticationStateProvider authStateProvider
<h3>Logging out...</h3>
@code {
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
var customAuthStateProvider = (CustomAuthenticationStateProvider) authStateProvider;
await customAuthStateProvider.UpdateAuthenticationStateAsync(null);
navManager.NavigateTo("/", true);
}
}

View file

@ -1,10 +1,13 @@
@page "/sys/register" @page "/sys/register"
@using ImageBoardServerApp.Util
@using ImageBoardServerApp.Data.Repository @using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Auth @using ImageBoardServerApp.Auth
@inject IJSRuntime js @inject IJSRuntime js
@inject AuthenticationStateProvider authStateProvider @inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager @inject NavigationManager navManager
<PageTitle>Register - Buttletboards</PageTitle>
<div class="login"> <div class="login">
<form> <form>
<RadzenFormField Text="Email" Variant="Variant.Outlined"> <RadzenFormField Text="Email" Variant="Variant.Outlined">
@ -19,7 +22,7 @@
<RadzenLabel Text="I am atleast 18 Years old." Component="is_18" /> <RadzenLabel Text="I am atleast 18 Years old." Component="is_18" />
<br/> <br/>
<br/> <br/>
<RadzenButton Click=@login Text="login" ButtonStyle="ButtonStyle.Secondary" /> <RadzenButton Click=@login Text="register" ButtonStyle="ButtonStyle.Secondary" />
</form> </form>
<br/> <br/>
<br/> <br/>
@ -64,14 +67,23 @@
Password = BCrypt.Net.BCrypt.HashPassword(Password), Password = BCrypt.Net.BCrypt.HashPassword(Password),
Role = "User", Role = "User",
TimeBanned = -1, TimeBanned = -1,
LastUsedName = "Anonymous" LastUsedName = "Anonymous",
BanReason = "Not banned",
ConfirmedEmail = false,
ResetPasswordExpiresAt = DateTimeOffset.Now.ToUnixTimeMilliseconds() + 300000,
ConfirmEmailToken = TheManager.getmd5Hash(),
ResetPasswordToken = "-1"
}; };
if (await UsersRepository.getUserByEmailAsync(Email) != null) if (await UsersRepository.getUserByEmailAsync(Email) != null)
{ {
msg = "This Email is already registered.";
return; return;
} }
await UsersRepository.createUserAsync(userToCreate); await UsersRepository.createUserAsync(userToCreate);
Postman.sendMail(Email, "Confirm Email", "");
var user = await UsersRepository.getUserByEmailRawAsync(Email); var user = await UsersRepository.getUserByEmailRawAsync(Email);
if (user == null) if (user == null)
{ {

View file

@ -0,0 +1,49 @@
@page "/sys/resetpw"
@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Util
<h3 class="headLogin">Reset your Password</h3>
<div class="login">
<form>
<RadzenFormField Text="Email" Variant="Variant.Outlined">
<RadzenTextBox @bind-Value="@Email" />
</RadzenFormField>
<br/>
<br/>
<RadzenButton Click=@resetPassword Text="Send reset Email" ButtonStyle="ButtonStyle.Secondary" />
<br/>
</form>
@if (msg != null)
{
<div>
<span class="msg">@msg</span>
</div>
}
</div>
@code {
private string Email { get; set; }
private string msg { get; set; }
private async void resetPassword()
{
Console.WriteLine("Resetting Password...");
var user = await UsersRepository.getUserByEmailRawAsync(Email);
if (user == null)
{
return;
}
var hash = TheManager.getmd5Hash();
user.ResetPasswordToken = hash;
user.ResetPasswordExpiresAt = DateTimeOffset.Now.ToUnixTimeMilliseconds() + 300000;
await UsersRepository.updateUserAsync(user);
Postman.sendMail(user.Email,
"Password Reset",
"Reset your Password using this link:\n" +
$"https://bulletboards.xyz/sys/click/resetpw/{user.UserID}/{hash}");
msg = "A reset email has been sent to the selected email, if that email is registered here. The link expires in 5 Minutes";
}
}

View file

@ -0,0 +1,14 @@
.headLogin{
text-align: center;
-webkit-text-size-adjust: auto;
}
.login{
text-align: center;
align-items:center;
font-style: unset;
}
.gaping{
alignment: left;
}

View file

@ -4,6 +4,8 @@
@inject AuthenticationStateProvider authStateProvider @inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager @inject NavigationManager navManager
<PageTitle>YOU - Bulletboards</PageTitle>
<AuthorizeView> <AuthorizeView>
<Authorized> <Authorized>
<h3>YOU</h3> <h3>YOU</h3>
@ -18,7 +20,7 @@
<input type="password" id="password" @bind="newPassword" /> <input type="password" id="password" @bind="newPassword" />
<a @onclick="changePassword" href="javascript:void(0)">[Change Password]</a> <a @onclick="changePassword" href="javascript:void(0)">[Change Password]</a>
<br/> <br/>
<a @onclick="logout" href="javascript:void(0)">[Logout]</a> <a href="/sys/logout">[Logout]</a>
</Authorized> </Authorized>
<NotAuthorized> <NotAuthorized>
<span>You are not logged in.</span> <span>You are not logged in.</span>
@ -42,13 +44,6 @@
} }
} }
private async Task logout()
{
var customAuthStateProvider = (CustomAuthenticationStateProvider) authStateProvider;
await customAuthStateProvider.UpdateAuthenticationStateAsync(null);
navManager.NavigateTo("/", true);
}
private async Task changeEmail() private async Task changeEmail()
{ {
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider; var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;