fix exception in update endpoint

This commit is contained in:
Elijah R 2024-04-05 08:54:32 -04:00
parent d8ba680d34
commit 13c6261915

View file

@ -66,53 +66,58 @@ public static class Routes
}, Utilities.JsonSerializerOptions); }, Utilities.JsonSerializerOptions);
} }
// Validate new username // Validate new username
if (!string.IsNullOrWhiteSpace(payload.username) && !Utilities.ValidateUsername(payload.username)) if (!string.IsNullOrWhiteSpace(payload.username))
{ {
return Results.Json(new UpdateResponse if (!Utilities.ValidateUsername(payload.username))
{ {
success = false, return Results.Json(new UpdateResponse
error = "Usernames can contain only numbers, letters, spaces, dashes, underscores, and dots, and must be between 3 and 20 characters." {
}, Utilities.JsonSerializerOptions); success = false,
error = "Usernames can contain only numbers, letters, spaces, dashes, underscores, and dots, and must be between 3 and 20 characters."
}, Utilities.JsonSerializerOptions);
}
// Make sure username isn't taken
var _user = await Program.Database.GetUser(payload.username);
if (_user != null)
{
context.Response.StatusCode = 400;
return Results.Json(new RegisterResponse
{
success = false,
error = "That username is taken."
}, Utilities.JsonSerializerOptions);
}
} }
// Validate new E-Mail // Validate new E-Mail
if (!string.IsNullOrWhiteSpace(payload.email) && !new EmailAddressAttribute().IsValid(payload.email)) if (!string.IsNullOrWhiteSpace(payload.email))
{ {
return Results.Json(new UpdateResponse if (!new EmailAddressAttribute().IsValid(payload.email))
{ {
success = false, return Results.Json(new UpdateResponse
error = "Malformed E-Mail address." {
}, Utilities.JsonSerializerOptions); success = false,
} error = "Malformed E-Mail address."
if (!string.IsNullOrWhiteSpace(payload.email) && Program.Config.Registration.EmailDomainWhitelist && }, Utilities.JsonSerializerOptions);
!Program.Config.Registration.AllowedEmailDomains.Contains(payload.email.Split("@")[1])) }
{ if (Program.Config.Registration.EmailDomainWhitelist && !Program.Config.Registration.AllowedEmailDomains.Contains(payload.email.Split("@")[1]))
return Results.Json(new UpdateResponse
{ {
success = false, return Results.Json(new UpdateResponse
error = "That E-Mail domain is not allowed." {
}, Utilities.JsonSerializerOptions); success = false,
} error = "That E-Mail domain is not allowed."
// Make sure username isn't taken }, Utilities.JsonSerializerOptions);
var _user = await Program.Database.GetUser(payload.username); }
if (_user != null) // Check if E-Mail is in use
{ var _user = await Program.Database.GetUser(email: payload.email);
context.Response.StatusCode = 400; if (_user != null)
return Results.Json(new RegisterResponse
{ {
success = false, context.Response.StatusCode = 400;
error = "That username is taken." return Results.Json(new RegisterResponse
}, Utilities.JsonSerializerOptions); {
} success = false,
// Check if E-Mail is in use error = "That E-Mail is already in use."
_user = await Program.Database.GetUser(email: payload.email); }, Utilities.JsonSerializerOptions);
if (_user != null) }
{
context.Response.StatusCode = 400;
return Results.Json(new RegisterResponse
{
success = false,
error = "That E-Mail is already in use."
}, Utilities.JsonSerializerOptions);
} }
// Validate new password // Validate new password
if (!string.IsNullOrWhiteSpace(payload.newPassword)) if (!string.IsNullOrWhiteSpace(payload.newPassword))