-
-
Notifications
You must be signed in to change notification settings - Fork 884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement email verification (fixes #219) #1956
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,8 +24,6 @@ use lemmy_db_schema::{ | |
site::Site, | ||
}, | ||
traits::{Crud, Followable, Joinable}, | ||
ListingType, | ||
SortType, | ||
}; | ||
use lemmy_db_views_actor::person_view::PersonViewSafe; | ||
use lemmy_utils::{ | ||
|
@@ -36,7 +34,7 @@ use lemmy_utils::{ | |
ConnectionId, | ||
LemmyError, | ||
}; | ||
use lemmy_websocket::{messages::CheckCaptcha, LemmyContext}; | ||
use lemmy_websocket::{email::send_verification_email, messages::CheckCaptcha, LemmyContext}; | ||
|
||
#[async_trait::async_trait(?Send)] | ||
impl PerformCrud for Register { | ||
|
@@ -49,16 +47,24 @@ impl PerformCrud for Register { | |
) -> Result<LoginResponse, LemmyError> { | ||
let data: &Register = self; | ||
|
||
// no email verification if the site is not setup yet | ||
let mut email_verification = false; | ||
|
||
// Make sure site has open registration | ||
if let Ok(site) = blocking(context.pool(), Site::read_simple).await? { | ||
if !site.open_registration { | ||
return Err(ApiError::err_plain("registration_closed").into()); | ||
} | ||
email_verification = site.require_email_verification; | ||
} | ||
|
||
password_length_check(&data.password)?; | ||
honeypot_check(&data.honeypot)?; | ||
|
||
if email_verification && data.email.is_none() { | ||
return Err(ApiError::err_plain("email_required").into()); | ||
} | ||
|
||
// Make sure passwords match | ||
if data.password != data.password_verify { | ||
return Err(ApiError::err_plain("passwords_dont_match").into()); | ||
|
@@ -124,22 +130,13 @@ impl PerformCrud for Register { | |
.map_err(|e| ApiError::err("user_already_exists", e))?; | ||
|
||
// Create the local user | ||
// TODO some of these could probably use the DB defaults | ||
let local_user_form = LocalUserForm { | ||
person_id: inserted_person.id, | ||
person_id: Some(inserted_person.id), | ||
email: Some(data.email.to_owned()), | ||
password_encrypted: data.password.to_owned(), | ||
password_encrypted: Some(data.password.to_owned()), | ||
show_nsfw: Some(data.show_nsfw), | ||
show_bot_accounts: Some(true), | ||
theme: Some("browser".into()), | ||
default_sort_type: Some(SortType::Active as i16), | ||
default_listing_type: Some(ListingType::Subscribed as i16), | ||
lang: Some("browser".into()), | ||
show_avatars: Some(true), | ||
show_scores: Some(true), | ||
show_read_posts: Some(true), | ||
show_new_post_notifs: Some(false), | ||
send_notifications_to_email: Some(false), | ||
email_verified: Some(!email_verification), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed this too. If email_verification is false, this will incorrectly get set to true, when no email has been actually verified. This should just be |
||
..LocalUserForm::default() | ||
}; | ||
|
||
let inserted_local_user = match blocking(context.pool(), move |conn| { | ||
|
@@ -228,13 +225,24 @@ impl PerformCrud for Register { | |
.map_err(|e| ApiError::err("community_moderator_already_exists", e))?; | ||
} | ||
|
||
// Return the jwt | ||
Ok(LoginResponse { | ||
jwt: Claims::jwt( | ||
// Log the user in directly if email verification is not required | ||
let jwt = if email_verification { | ||
send_verification_email( | ||
inserted_local_user.id, | ||
// we check at the beginning of this method that email is set | ||
&inserted_local_user.email.expect("email was provided"), | ||
&inserted_person.name, | ||
context, | ||
) | ||
.await?; | ||
None | ||
Comment on lines
+228
to
+238
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good, I'll have to remember to create a translation string for "Your application has been sent" or something like that, for when jwt is The other possibility is to add an optional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm doing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this now on my PR, so don't worry about this one. |
||
} else { | ||
Some(Claims::jwt( | ||
inserted_local_user.id.0, | ||
&context.secret().jwt_secret, | ||
&context.settings().hostname, | ||
)?, | ||
}) | ||
)?) | ||
}; | ||
Ok(LoginResponse { jwt }) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,8 +47,6 @@ impl PerformCrud for DeleteAccount { | |
}) | ||
.await??; | ||
|
||
Ok(LoginResponse { | ||
jwt: data.auth.to_owned(), | ||
}) | ||
Ok(LoginResponse { jwt: None }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This never really made sense in the first place, and now we'll get conflicting responses. Could you create |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of mut, you could probably do
let email_verification = if let ...