feat: finished email confirmation, finished Password Reset

!fix: The user auth system now uses the id, not the email

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-06-09 09:45:14 +02:00
parent 828f784fc8
commit e8e97b2cd9
11 changed files with 89 additions and 20 deletions

View file

@ -27,7 +27,7 @@ public class CustomAuthenticationStateProvider : AuthenticationStateProvider
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
{
//new Claim(ClaimTypes.Email, userSession.Email),
new Claim(ClaimTypes.Name, userSession.Email),
new Claim(ClaimTypes.Name, userSession.UserID.ToString()),
new Claim(ClaimTypes.Role, userSession.Role)
}, "CustomAuth"));
return await Task.FromResult(new AuthenticationState(claimsPrincipal));
@ -47,7 +47,7 @@ public class CustomAuthenticationStateProvider : AuthenticationStateProvider
await _sessionStorage.SetAsync("UserSession", session);
claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
{
new Claim(ClaimTypes.Email, session.Email),
new Claim(ClaimTypes.Email, session.UserID.ToString()),
new Claim(ClaimTypes.Email, session.Role)
}));
}

View file

@ -18,7 +18,13 @@ public static class UsersRepository
public static async Task<UserData> getUserByIdAsync(int userId)
{
await using var db = new AppDBContext();
return await db.Users.FirstOrDefaultAsync(user => user.UserID == userId);
return await db.Users
.Where(user => user.UserID == userId)
.Include(user => user.SubmittedReports)
.Include(user => user.Posts)
.Include(user => user.Comments)
.Include(user => user.RecivedReports)
.FirstOrDefaultAsync();
}
public static async Task<UserData> getUserByEmailRawAsync(string email)

View file

@ -1,6 +1,61 @@
@page "/sys/click/confirmmail/{userid}/{email}/{token}"
<h3>Confirming your Email...</h3>
@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Data.Repository
<h3>Confirm your Email</h3>
<span>@msg</span>
@code {
private string msg { get; set; } = "Loading...";
[Parameter]
[Required]
public string userid { get; set; }
[Parameter]
[Required]
public string email { get; set; }
[Parameter]
[Required]
public string token { get; set; }
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
if (!int.TryParse(userid, out _))
{
msg = "This is not a valid id.";
return;
}
var user = await UsersRepository.getUserByIdAsync(int.Parse(userid));
if (user == null)
{
msg = "Could not find user.";
return;
}
if (user.Email != email)
{
msg = "The email does not match.";
return;
}
if (user.ConfirmEmailToken != token)
{
msg = "The token is not correct.";
return;
}
user.ConfirmEmailToken = "0";
user.ConfirmedEmail = true;
await UsersRepository.updateUserAsync(user);
msg = "The email has been confirmed.";
}
}

View file

@ -16,12 +16,18 @@
<RadzenButton Click=@reset Text="reset" ButtonStyle="ButtonStyle.Secondary" />
<br/>
</form>
@if (msg != null)
{
<span>@msg</span>
}
</div>
@code {
private string Password { get; set; }
private string msg { get; set; }
[Parameter]
[Required]
public string userid { get; set; }
@ -32,7 +38,7 @@
public async void reset()
{
if (int.TryParse(userid, out _))
if (!int.TryParse(userid, out _))
return;
var user = await UsersRepository.getUserByIdAsync(int.Parse(userid));
if (user == null)
@ -44,8 +50,11 @@
Console.WriteLine("Resetting a password...");
user.Password = Password = BCrypt.Net.BCrypt.HashPassword(Password);
user.ResetPasswordToken = "-1";
await UsersRepository.updateUserAsync(user);
msg = "Your Password has been updated.";
}
}

View file

@ -49,7 +49,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr == null)
{
return;
@ -69,7 +69,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr == null)
{
return;

View file

@ -58,7 +58,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr == null)
{
return;
@ -100,7 +100,7 @@
return;
}
var reportID = await ReportsRepository.createReportAsync(reportData);
await ReportsRepository.createReportAsync(reportData);
js.InvokeVoidAsync("window.close");
}
}

View file

@ -124,7 +124,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr.Role != "User" || comment.UserID == foundusr.UserID)
{
await TheManager.deleteComment(comment);
@ -148,7 +148,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr != null && (foundusr.Role != "User" || comment.UserID == foundusr.UserID))
{
canDel = true;

View file

@ -63,7 +63,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr == null)
{
hasErr = true;
@ -90,7 +90,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr == null)
{
hasErr = true;

View file

@ -69,7 +69,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr == null)
{
hasErr = true;
@ -98,7 +98,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr == null)
{
hasErr = true;

View file

@ -157,7 +157,7 @@
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
if (foundusr.Role != "User" || post.UserID == foundusr.UserID)
{
await TheManager.deleteThread(post);
@ -183,11 +183,10 @@
{
await base.OnParametersSetAsync();
value = TheManager.getBumpValue(post) + "v";
string boardTag = post.Board;
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
UserData foundusr = await UsersRepository.getUserByIdAsync(int.Parse(usr.Identity.Name));
reportURL = $"/sys/report/op/{post.Board}/{post.PostID}";
threadURL = $"/{post.Board}/thread/{post.PostID}";

View file

@ -28,7 +28,7 @@
var user = await cauthStateProvder.GetAuthenticationStateAsync();
if (user.User.Identity.IsAuthenticated)
{
mail = user.User.Identity.Name;
mail = $"Welcome User # {user.User.Identity.Name}";
}
}
}