Initial Implementation of AuthService

This commit is contained in:
2024-02-25 02:10:43 -03:00
parent 05dc487099
commit 11f01a6156
8 changed files with 75 additions and 37 deletions

View File

@@ -0,0 +1,35 @@
use reqwest::header::AUTHORIZATION;
use crate::config::config_auth::ConfigAuth;
use crate::model::send_message::MessageAuthor;
#[derive(Clone)]
pub struct AuthService {
auth_url: String,
}
impl AuthService {
pub fn new(config_auth: ConfigAuth) -> Self {
AuthService {
auth_url: config_auth.auth_url,
}
}
pub async fn validate_token(&self, token: &str) -> Option<MessageAuthor> {
println!("Received token: {}", token);
let validation_url = format!("{}/user/login/validate", self.auth_url);
let client = reqwest::Client::new();
let response = client.post(validation_url.as_str())
.header(AUTHORIZATION, format!("Bearer {}", token))
.send().await.unwrap();
if response.status().is_success() {
let text = response.text().await.unwrap();
return serde_json::from_str(&text).unwrap();
}
None
}
}