Newer
Older
use crate::AppState;
use actix_web::{
get, post,
// TODO return admin static web view with js application
actix_web::HttpResponse::Ok().body("Welcome Admin")
}
#[derive(serde::Deserialize)]
struct Credentials {
username: String,
password: String,
}
#[post("/login")]
async fn admin_authenticate(
credentials: Form<Credentials>,
let (admin_username, admin_pwd, cookie_name) = {
let app_state = app_state
.read()
.expect("Couldn't read to RwLock<AppState>.");
(
app_state.config.admin_username.to_owned(),
app_state.config.admin_pwd.to_owned(),
app_state.config.admin_cookie_name.to_owned(),
)
};
if admin_username.eq(&credentials.username) && admin_pwd.eq(&credentials.password) {
let mut app_state = app_state
.write()
.expect("Couldn't acquire write lock for RwLock<AppState>");
app_state.admin_auth_token.update_value();
let cookie = app_state.admin_auth_token.generate_cookie();
return HttpResponse::Accepted().cookie(cookie).finish();
} else {
let mut res = HttpResponse::Unauthorized().finish();
match req.cookie(&cookie_name) {
Some(_) => {
res.del_cookie(&cookie_name);
return res;
}
None => return res,
}
}
}
#[get("/login")]
// TODO create a module with built-in admin static views
<html lang='en' prefix='og: https://ogp.me/ns#'>
<head>
<meta charset='UTF-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Krutacea - Admin Login</title>
<link rel='stylesheet' href='/assets/default/admin.css'>
<body>
<form id='admin-login-form'>
<div>
<label for='username'>Admin Id</label>
<input type='text' name='username'/>
</div>
<div>
<label for='password'>Password</label>
<input type='password' name='password' />
</div>
<input type='submit' />
</form>
</body>
<script src='/assets/default/admin.js'></script>