From 071c773412ca6573076c360c065b696fc71df0da Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 13 Jul 2020 13:00:02 +0200 Subject: [PATCH] Can check if an email address exists or not --- src/api_data/mod.rs | 3 ++- src/api_data/res_check_email_exists.rs | 16 ++++++++++++++++ src/controllers/account_controller.rs | 8 ++++++++ src/controllers/routes.rs | 1 + src/helpers/account_helper.rs | 8 ++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/api_data/res_check_email_exists.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 7fde199..f229695 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -42,4 +42,5 @@ pub mod res_number_unread_notifications; pub mod res_count_all_unreads; pub mod notification_api; pub mod user_membership_api; -mod type_container_api; \ No newline at end of file +mod type_container_api; +pub mod res_check_email_exists; \ No newline at end of file diff --git a/src/api_data/res_check_email_exists.rs b/src/api_data/res_check_email_exists.rs new file mode 100644 index 0000000..ba73a6a --- /dev/null +++ b/src/api_data/res_check_email_exists.rs @@ -0,0 +1,16 @@ +//! # Check if email exists result +//! +//! @author Pierre Hubert +use serde::Serialize; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct ResCheckEmailExists { + exists: bool +} + +impl ResCheckEmailExists { + pub fn new(exists: bool) -> ResCheckEmailExists { + ResCheckEmailExists { exists } + } +} \ No newline at end of file diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index f6982b1..a6a3861 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -1,5 +1,6 @@ use crate::api_data::current_user_id::CurrentUserID; use crate::api_data::login_success::LoginSuccess; +use crate::api_data::res_check_email_exists::ResCheckEmailExists; use crate::controllers::routes::RequestResult; use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::account_helper; @@ -51,4 +52,11 @@ pub fn disconnect_all_devices(r: &mut HttpRequestHandler) -> RequestResult { /// Get current user ID pub fn user_id(request: &mut HttpRequestHandler) -> RequestResult { request.set_response(CurrentUserID::new(&request.user_id()?)) +} + +/// Check out whether an email address exists or not +pub fn exists_mail(r: &mut HttpRequestHandler) -> RequestResult { + let email = r.post_email("email")?; + + r.set_response(ResCheckEmailExists::new(account_helper::exists_mail(&email)?)) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index fd380a6..f60ca25 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -74,6 +74,7 @@ pub fn get_routes() -> Vec { Route::post("/account/disconnect_all_devices", Box::new(account_controller::disconnect_all_devices)), Route::post("/account/id", Box::new(account_controller::user_id)), Route::post("/user/getCurrentUserID", Box::new(account_controller::user_id)), + Route::post_without_login("/account/exists_email", Box::new(account_controller::exists_mail)), // User controller Route::post_without_login("/user/getInfo", Box::new(user_controller::get_single)), diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index e8257bd..4966a9f 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -77,6 +77,14 @@ pub fn get_user_by_login_token(token: &str, client: &APIClient) -> ResultBoxErro ) } +/// Check out whether an email address exists or not +pub fn exists_mail(mail: &str) -> ResultBoxError { + database::QueryInfo::new(USERS_TABLE) + .cond("mail", mail) + .exec_count() + .map(|r| r > 0) +} + /// Destroy a given user login tokens pub fn destroy_login_tokens(id: &UserID, client: &APIClient) -> ResultBoxError<()> { database::delete(DeleteQuery::new(USER_ACCESS_TOKENS_TABLE)