1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Can get current user ID

This commit is contained in:
2020-05-24 17:57:47 +02:00
parent 3b1b377d82
commit 75024dbca6
7 changed files with 82 additions and 5 deletions

View File

@ -4,12 +4,13 @@ use std::error::Error;
use serde::Serialize;
use crate::data::error::{ResultBoxError, ExecError};
use std::collections::HashMap;
use crate::helpers::api_helper;
use crate::helpers::{api_helper, account_helper};
use actix_web::http::{HeaderName, HeaderValue};
use std::str::FromStr;
use crate::data::config::conf;
use crate::data::api_client::APIClient;
use crate::api_data::http_error::HttpError;
use crate::data::user::UserID;
/// Http request handler
///
@ -40,6 +41,7 @@ pub struct HttpRequestHandler {
response: Option<web::HttpResponse>,
headers: HashMap<String, String>,
client: Option<APIClient>,
curr_user_id: Option<UserID>,
}
impl HttpRequestHandler {
@ -51,6 +53,7 @@ impl HttpRequestHandler {
response: None,
headers: HashMap::new(),
client: None,
curr_user_id: None,
}
}
@ -175,8 +178,8 @@ impl HttpRequestHandler {
}
}
/// Check login tokens
pub fn check_client_token(&mut self) -> Result<(), Box<dyn Error>> {
/// Check API client tokens
pub fn check_client_token(&mut self) -> RequestResult {
let api_name = self.post_string("serviceName")?;
let api_token = self.post_string("serviceToken")?;
@ -209,6 +212,28 @@ impl HttpRequestHandler {
Ok(())
}
/// Check login token
pub fn check_user_token(&mut self) -> RequestResult {
let token = self.post_string("userToken1")?;
// Find user
let user_id = self.ok_or_bad_request(
account_helper::get_user_by_login_token(&token, self.api_client()),
"Please check your login tokens!")?;
self.curr_user_id = Some(user_id);
Ok(())
}
/// Get user ID. This function assess that a user ID is available to continue
pub fn user_id(&self) -> ResultBoxError<UserID> {
match self.curr_user_id {
Some(s) => Ok(s),
None => Err(ExecError::boxed_new("Could not get required user ID!"))
}
}
/// Get an email included in the request
pub fn post_email(&mut self, name: &str) -> ResultBoxError<String> {
let mail = self.post_string(name)?;