aboutsummaryrefslogtreecommitdiff
path: root/src/controller/login.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller/login.rs')
-rw-r--r--src/controller/login.rs35
1 files changed, 20 insertions, 15 deletions
diff --git a/src/controller/login.rs b/src/controller/login.rs
index a1bf466..f7e0695 100644
--- a/src/controller/login.rs
+++ b/src/controller/login.rs
@@ -3,9 +3,8 @@ use http_body_util::Full;
use hyper::body::Bytes;
use hyper::header::SET_COOKIE;
use hyper::Response;
-use sqlx::sqlite::SqlitePool;
use std::collections::HashMap;
-use tera::{Context, Tera};
+use tokio_rusqlite::Connection;
use crate::controller::utils::with_headers;
use crate::controller::wallet::Wallet;
@@ -18,14 +17,15 @@ use crate::validation;
pub async fn page(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
error: Option<&str>,
) -> Response<Full<Bytes>> {
let connected_user: Option<User> = None;
- let mut context = Context::new();
- context.insert("connected_user", &connected_user);
- context.insert("error", &error);
+ let context = minijinja::context!(
+ connected_user => &connected_user,
+ error => &error
+ );
utils::template(assets, templates, "login.html", context)
}
@@ -33,20 +33,22 @@ pub async fn page(
pub async fn login(
config: &Config,
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
form: HashMap<String, String>,
- pool: SqlitePool,
+ db_conn: Connection,
) -> Response<Full<Bytes>> {
match validation::login::login(&form) {
Some(login) => {
- match db::users::get_password_hash(&pool, login.email.clone()).await
+ match db::users::get_password_hash(&db_conn, login.email.clone())
+ .await
{
Some(hash) => match bcrypt::verify(login.password, &hash) {
Ok(true) => {
- let login_token = cookie::generate_token();
+ // TODO: error handling
+ let login_token = cookie::generate_token().unwrap();
if db::users::set_login_token(
- &pool,
+ &db_conn,
login.email,
login_token.clone().to_string(),
)
@@ -75,7 +77,10 @@ pub async fn login(
}
Ok(false) => not_authorized(assets, templates).await,
Err(err) => {
- log::error!("Error verifying bcrypt password: {:?}", err);
+ log::error!(
+ "Error verifying bcrypt password: {:?}",
+ err
+ );
server_error(assets, templates, "Erreur serveur").await
}
},
@@ -88,7 +93,7 @@ pub async fn login(
async fn server_error(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
msg: &str,
) -> Response<Full<Bytes>> {
page(assets, templates, Some(msg)).await
@@ -96,7 +101,7 @@ async fn server_error(
async fn not_authorized(
assets: &HashMap<String, String>,
- templates: &Tera,
+ templates: &minijinja::Environment<'_>,
) -> Response<Full<Bytes>> {
page(
assets,
@@ -107,7 +112,7 @@ async fn not_authorized(
}
pub async fn logout(config: &Config, wallet: &Wallet) -> Response<Full<Bytes>> {
- if db::users::remove_login_token(&wallet.pool, wallet.user.id).await {
+ if db::users::remove_login_token(&wallet.db_conn, wallet.user.id).await {
with_headers(
utils::redirect("/"),
vec![(SET_COOKIE, &cookie::logout(config))],