mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-15 06:02:28 +00:00
Refactor Database into DatabaseContext Moved into separate folder so it actually has a namespace instead sitting in the root
108 lines
No EOL
3.6 KiB
C#
108 lines
No EOL
3.6 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using JetBrains.Annotations;
|
|
using LBPUnion.ProjectLighthouse.Configuration;
|
|
using LBPUnion.ProjectLighthouse.Database;
|
|
using LBPUnion.ProjectLighthouse.Extensions;
|
|
using LBPUnion.ProjectLighthouse.Helpers;
|
|
using LBPUnion.ProjectLighthouse.Localization.StringLists;
|
|
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Login;
|
|
|
|
public class RegisterForm : BaseLayout
|
|
{
|
|
public RegisterForm(DatabaseContext database) : base(database)
|
|
{ }
|
|
|
|
public string? Error { get; private set; }
|
|
|
|
public string? Username { get; set; }
|
|
|
|
[UsedImplicitly]
|
|
[SuppressMessage("ReSharper", "SpecifyStringComparison")]
|
|
public async Task<IActionResult> OnPost(string username, string password, string confirmPassword, string emailAddress)
|
|
{
|
|
if (this.Database.UserFromWebRequest(this.Request) != null) return this.Redirect("~/");
|
|
|
|
if (!ServerConfiguration.Instance.Authentication.RegistrationEnabled) return this.NotFound();
|
|
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
{
|
|
this.Error = this.Translate(ErrorStrings.UsernameInvalid);
|
|
return this.Page();
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(password))
|
|
{
|
|
this.Error = this.Translate(ErrorStrings.PasswordInvalid);
|
|
return this.Page();
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(emailAddress) && ServerConfiguration.Instance.Mail.MailEnabled)
|
|
{
|
|
this.Error = this.Translate(ErrorStrings.EmailInvalid);
|
|
return this.Page();
|
|
}
|
|
|
|
if (password != confirmPassword)
|
|
{
|
|
this.Error = this.Translate(ErrorStrings.PasswordDoesntMatch);
|
|
return this.Page();
|
|
}
|
|
|
|
User? existingUser = await this.Database.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower());
|
|
if (existingUser != null)
|
|
{
|
|
this.Error = this.Translate(ErrorStrings.UsernameTaken);
|
|
return this.Page();
|
|
}
|
|
|
|
if (ServerConfiguration.Instance.Mail.MailEnabled &&
|
|
await this.Database.Users.AnyAsync(u => u.EmailAddress != null && u.EmailAddress.ToLower() == emailAddress.ToLower()))
|
|
{
|
|
this.Error = this.Translate(ErrorStrings.EmailTaken);
|
|
return this.Page();
|
|
}
|
|
|
|
if (!await this.Request.CheckCaptchaValidity())
|
|
{
|
|
this.Error = this.Translate(ErrorStrings.CaptchaFailed);
|
|
return this.Page();
|
|
}
|
|
|
|
User user = await this.Database.CreateUser(username, CryptoHelper.BCryptHash(password), emailAddress);
|
|
|
|
WebToken webToken = new()
|
|
{
|
|
UserId = user.UserId,
|
|
UserToken = CryptoHelper.GenerateAuthToken(),
|
|
ExpiresAt = DateTime.Now + TimeSpan.FromDays(7),
|
|
};
|
|
|
|
this.Database.WebTokens.Add(webToken);
|
|
await this.Database.SaveChangesAsync();
|
|
|
|
this.Response.Cookies.Append("LighthouseToken", webToken.UserToken);
|
|
|
|
return ServerConfiguration.Instance.Mail.MailEnabled ?
|
|
this.Redirect("~/login/sendVerificationEmail") :
|
|
this.Redirect("~/");
|
|
}
|
|
|
|
[UsedImplicitly]
|
|
[SuppressMessage("ReSharper", "SpecifyStringComparison")]
|
|
public IActionResult OnGet()
|
|
{
|
|
this.Error = string.Empty;
|
|
if (!ServerConfiguration.Instance.Authentication.RegistrationEnabled)
|
|
{
|
|
return this.NotFound();
|
|
}
|
|
|
|
return this.Page();
|
|
}
|
|
} |