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