Improve errors when email is already in use (#4014)

Fixes #1485

Also fixes an issue where email_verified was being set to true regardless of whether the oauth provider provides an email (thus indicating that a null email is verified)
This commit is contained in:
Emma Alexia 2025-07-17 21:59:48 -04:00 committed by GitHub
parent 013ba4d86d
commit 0c3e23db96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 6 deletions

View File

@ -43,7 +43,9 @@ pub enum AuthenticationError {
InvalidAuthMethod, InvalidAuthMethod,
#[error("GitHub Token from incorrect Client ID")] #[error("GitHub Token from incorrect Client ID")]
InvalidClientId, InvalidClientId,
#[error("User email/account is already registered on Modrinth")] #[error(
"User email is already registered on Modrinth. Try 'Forgot password' to access your account."
)]
DuplicateUser, DuplicateUser,
#[error("Invalid state sent, you probably need to get a new websocket")] #[error("Invalid state sent, you probably need to get a new websocket")]
SocketError, SocketError,

View File

@ -223,8 +223,8 @@ impl TempUser {
stripe_customer_id: None, stripe_customer_id: None,
totp_secret: None, totp_secret: None,
username, username,
email: self.email, email: self.email.clone(),
email_verified: true, email_verified: self.email.is_some(),
avatar_url, avatar_url,
raw_avatar_url, raw_avatar_url,
bio: self.bio, bio: self.bio,
@ -1419,15 +1419,15 @@ pub async fn create_account_with_password(
.hash_password(new_account.password.as_bytes(), &salt)? .hash_password(new_account.password.as_bytes(), &salt)?
.to_string(); .to_string();
if crate::database::models::DBUser::get_by_email( if !crate::database::models::DBUser::get_by_case_insensitive_email(
&new_account.email, &new_account.email,
&**pool, &**pool,
) )
.await? .await?
.is_some() .is_empty()
{ {
return Err(ApiError::InvalidInput( return Err(ApiError::InvalidInput(
"Email is already registered on Modrinth!".to_string(), "Email is already registered on Modrinth! Try 'Forgot password' to access your account.".to_string(),
)); ));
} }
@ -2220,6 +2220,18 @@ pub async fn set_email(
.await? .await?
.1; .1;
if !crate::database::models::DBUser::get_by_case_insensitive_email(
&email.email,
&**pool,
)
.await?
.is_empty()
{
return Err(ApiError::InvalidInput(
"Email is already registered on Modrinth! Try 'Forgot password' in incognito to access and delete your other account.".to_string(),
));
}
let mut transaction = pool.begin().await?; let mut transaction = pool.begin().await?;
sqlx::query!( sqlx::query!(