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

47 lines
1.2 KiB
Rust
Raw Normal View History

2020-05-23 17:17:48 +00:00
use serde::{Deserialize, Serialize};
2020-05-21 13:43:53 +00:00
use crate::data::http_request_handler::HttpRequestHandler;
use crate::controllers::routes::RequestResult;
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
2020-05-23 17:17:48 +00:00
#[derive(Serialize, Deserialize)]
struct LoginTokens {
token1: String,
token2: String,
}
#[derive(Serialize, Deserialize)]
struct LoginSuccess {
tokens: LoginTokens
}
2020-05-21 13:43:53 +00:00
/// 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) => {
request.set_response(LoginSuccess {
tokens: LoginTokens {
token1: t,
token2: "dummy_data".to_string()
}
})
},
Err(e) => {
// TODO : limit request
println!("Error on login: {}", e);
request.forbidden("Invalid email address / password!".to_string())
},
}
2020-05-21 13:43:53 +00:00
}