2020-06-29 13:53:39 +00:00
|
|
|
use crate::api_data::current_user_id::CurrentUserID;
|
2020-05-24 11:09:50 +00:00
|
|
|
use crate::api_data::login_success::LoginSuccess;
|
2020-05-21 13:43:53 +00:00
|
|
|
use crate::controllers::routes::RequestResult;
|
2020-05-24 11:09:50 +00:00
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
2020-05-23 17:17:48 +00:00
|
|
|
use crate::helpers::account_helper;
|
2020-05-21 13:43:53 +00:00
|
|
|
|
|
|
|
/// Account controller
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
/// Sign in user
|
2020-05-23 15:09:28 +00:00
|
|
|
pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let email = request.post_email("userMail")?;
|
|
|
|
let password = request.post_string_opt("userPassword", 3, true)?;
|
|
|
|
|
|
|
|
// TODO : limit request
|
|
|
|
|
|
|
|
// Authenticate user
|
2020-05-23 17:17:48 +00:00
|
|
|
let token = account_helper::login_user(
|
|
|
|
&email, &password, request.api_client());
|
2020-05-23 15:09:28 +00:00
|
|
|
|
2020-05-23 17:17:48 +00:00
|
|
|
match token {
|
|
|
|
Ok(t) => {
|
2020-05-24 11:09:50 +00:00
|
|
|
request.set_response(LoginSuccess::new(&t))
|
|
|
|
}
|
2020-05-23 17:17:48 +00:00
|
|
|
Err(e) => {
|
|
|
|
// TODO : limit request
|
|
|
|
println!("Error on login: {}", e);
|
|
|
|
request.forbidden("Invalid email address / password!".to_string())
|
2020-05-24 11:09:50 +00:00
|
|
|
}
|
2020-05-23 17:17:48 +00:00
|
|
|
}
|
2020-05-24 15:57:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:19:07 +00:00
|
|
|
/// Sign out user
|
|
|
|
pub fn logout_user(request: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
account_helper::destroy_login_tokens(
|
2020-06-25 08:08:34 +00:00
|
|
|
&request.user_id()?,
|
2020-06-29 13:53:39 +00:00
|
|
|
request.api_client(),
|
2020-05-24 17:19:07 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
request.success("User disconnected.")
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:57:47 +00:00
|
|
|
/// Get current user ID
|
|
|
|
pub fn user_id(request: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-25 08:08:34 +00:00
|
|
|
request.set_response(CurrentUserID::new(&request.user_id()?))
|
2020-05-21 13:43:53 +00:00
|
|
|
}
|