Add password reset page

This commit is contained in:
jvyden 2021-11-27 19:45:05 -05:00
commit fa8e7a6b24
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
5 changed files with 86 additions and 44 deletions

View file

@ -0,0 +1,36 @@
@page "/passwordReset"
@model LBPUnion.ProjectLighthouse.Pages.PasswordResetPage
@{
Layout = "Layouts/BaseLayout";
}
<script src="https://geraintluff.github.io/sha256/sha256.min.js"></script>
<script>
function onSubmit(form) {
const password = form['password'];
const confirmPassword = form['confirmPassword'];
password.value = sha256(password.value);
confirmPassword.value = sha256(confirmPassword.value);
return true;
}
</script>
<h1>Password Reset</h1>
<form onsubmit="return onSubmit(this)">
<div class="ui left labeled input">
<label for="password" class="ui blue label">Password: </label>
<input type="password" name="password" id="password">
</div><br><br>
<div class="ui left labeled input">
<label for="password" class="ui blue label">Confirm Password: </label>
<input type="password" name="confirmPassword" id="confirmPassword">
</div><br><br><br>
<input type="submit" value="Reset password and continue" id="submit" class="ui green button"><br>
</form>

View file

@ -0,0 +1,38 @@
#nullable enable
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
namespace LBPUnion.ProjectLighthouse.Pages
{
public class PasswordResetPage : BaseLayout
{
public PasswordResetPage(Database database) : base(database)
{}
public bool WasResetRequest { get; private set; }
public async Task<IActionResult> OnGet([FromQuery] string password, [FromQuery] string confirmPassword)
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
this.WasResetRequest = !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(confirmPassword);
if (this.WasResetRequest)
{
if (password != confirmPassword) return this.BadRequest();
user.Password = HashHelper.BCryptHash(password);
user.PasswordResetRequired = false;
await this.Database.SaveChangesAsync();
return this.Redirect("~/");
}
return this.Page();
}
}
}

View file

@ -5,33 +5,9 @@
Layout = "Layouts/BaseLayout";
}
<script src="https://geraintluff.github.io/sha256/sha256.min.js"></script>
<script>
function onSubmit(form) {
const password = form['password'];
const confirmPassword = form['confirmPassword'];
password.value = sha256(password.value);
confirmPassword.value = sha256(confirmPassword.value);
return true;
}
</script>
<h1>Password Reset Required</h1>
<p>An admin has deemed it necessary that you reset your password. Please do so.</p>
<form onsubmit="return onSubmit(this)">
<div class="ui left labeled input">
<label for="password" class="ui blue label">Password: </label>
<input type="password" name="password" id="password">
</div><br><br>
<div class="ui left labeled input">
<label for="password" class="ui blue label">Confirm Password: </label>
<input type="password" name="confirmPassword" id="confirmPassword">
</div><br><br><br>
<input type="submit" value="Reset password and continue" id="submit" class="ui green button"><br>
</form>
<a href="/passwordReset">
<div class="ui blue button">Reset Password</div>
</a>

View file

@ -1,7 +1,6 @@
#nullable enable
using System.Threading.Tasks;
using JetBrains.Annotations;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
@ -15,25 +14,11 @@ namespace LBPUnion.ProjectLighthouse.Pages
public bool WasResetRequest { get; private set; }
public async Task<IActionResult> OnGet([FromQuery] string password, [FromQuery] string confirmPassword)
public async Task<IActionResult> OnGet()
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
if (!user.PasswordResetRequired) return this.Redirect("~/resetPassword");
this.WasResetRequest = !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(confirmPassword);
if (this.WasResetRequest)
{
if (password != confirmPassword) return this.BadRequest();
user.Password = HashHelper.BCryptHash(password);
user.PasswordResetRequired = false;
await this.Database.SaveChangesAsync();
return this.Redirect("~/");
}
if (!user.PasswordResetRequired) return this.Redirect("~/passwordReset");
return this.Page();
}

View file

@ -40,6 +40,13 @@
</a>
}
}
@if (Model.ProfileUser == Model.User)
{
<a class="ui blue button" href="/passwordReset">
<i class="key icon"></i>
<span>Reset Password</span>
</a>
}
</div>
<div class="eight wide column">
<div class="ui blue segment">