2022-03-30 10:41:22 +00:00
|
|
|
use actix::Addr;
|
|
|
|
use actix_web::{HttpResponse, Responder, web};
|
2022-03-30 08:29:10 +00:00
|
|
|
use askama::Template;
|
|
|
|
|
2022-03-30 10:41:22 +00:00
|
|
|
use crate::actors::users_actor::{LoginResult, UsersActor};
|
|
|
|
use crate::actors::users_actor;
|
2022-03-30 08:29:10 +00:00
|
|
|
use crate::constants::APP_NAME;
|
|
|
|
|
2022-03-30 09:00:20 +00:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "base_login_page.html")]
|
|
|
|
struct BaseLoginPage {
|
|
|
|
danger: String,
|
|
|
|
success: String,
|
|
|
|
page_title: &'static str,
|
|
|
|
app_name: &'static str,
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:29:10 +00:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "login.html")]
|
2022-03-30 09:00:20 +00:00
|
|
|
struct LoginTemplate {
|
|
|
|
_parent: BaseLoginPage,
|
2022-03-30 10:41:22 +00:00
|
|
|
login: String,
|
2022-03-30 08:29:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 10:41:22 +00:00
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
pub struct LoginRequest {
|
|
|
|
login: String,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Authenticate user
|
|
|
|
pub async fn login_route(users: web::Data<Addr<UsersActor>>,
|
|
|
|
req: Option<web::Form<LoginRequest>>) -> impl Responder {
|
|
|
|
let mut danger = String::new();
|
|
|
|
let mut login = String::new();
|
|
|
|
|
|
|
|
// Try to authenticate user
|
|
|
|
if let Some(req) = &req {
|
|
|
|
login = req.login.clone();
|
|
|
|
let response: LoginResult = users.send(users_actor::LoginRequest {
|
|
|
|
login: login.clone(),
|
|
|
|
password: req.password.clone(),
|
|
|
|
}).await.unwrap();
|
|
|
|
|
|
|
|
danger = format!("{:?}", response)
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:29:10 +00:00
|
|
|
|
2022-03-30 09:00:20 +00:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/html")
|
|
|
|
.body(LoginTemplate {
|
|
|
|
_parent: BaseLoginPage {
|
|
|
|
page_title: "Login",
|
2022-03-30 10:41:22 +00:00
|
|
|
danger,
|
2022-03-30 09:00:20 +00:00
|
|
|
success: "".to_string(),
|
|
|
|
app_name: APP_NAME,
|
|
|
|
},
|
2022-03-30 10:41:22 +00:00
|
|
|
login,
|
2022-03-30 09:00:20 +00:00
|
|
|
}.render().unwrap())
|
2022-03-30 08:29:10 +00:00
|
|
|
}
|