1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-23 13:59:21 +00:00
comunicapiv3/src/controllers/account_controller.rs

47 lines
1.3 KiB
Rust

use crate::api_data::login_success::LoginSuccess;
use crate::controllers::routes::RequestResult;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::account_helper;
use crate::api_data::current_user_id::CurrentUserID;
/// Account controller
///
/// @author Pierre Hubert
/// Sign in user
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
let token = account_helper::login_user(
&email, &password, request.api_client());
match token {
Ok(t) => {
request.set_response(LoginSuccess::new(&t))
}
Err(e) => {
// TODO : limit request
println!("Error on login: {}", e);
request.forbidden("Invalid email address / password!".to_string())
}
}
}
/// Sign out user
pub fn logout_user(request: &mut HttpRequestHandler) -> RequestResult {
account_helper::destroy_login_tokens(
&request.user_id()?,
request.api_client()
)?;
request.success("User disconnected.")
}
/// Get current user ID
pub fn user_id(request: &mut HttpRequestHandler) -> RequestResult {
request.set_response(CurrentUserID::new(&request.user_id()?))
}