From ee9597e83fbb148baf12101b8c827ffa2258e603 Mon Sep 17 00:00:00 2001 From: Pierre Jarriges <pierre.jarriges@tutanota.com> Date: Mon, 12 Sep 2022 13:26:50 +0200 Subject: [PATCH] wip admin web view --- default_static/admin.css | 18 ++++++++++++++ default_static/admin.js | 10 ++++++++ default_static/script.js | 11 +-------- src/middleware/authentication.rs | 2 +- src/service/admin.rs | 42 +++++++++++++++++++------------- 5 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 default_static/admin.css create mode 100644 default_static/admin.js diff --git a/default_static/admin.css b/default_static/admin.css new file mode 100644 index 0000000..97d8b53 --- /dev/null +++ b/default_static/admin.css @@ -0,0 +1,18 @@ +*+* { + box-sizing: border-box; + font-family: Lato, Arial, Helvetica, sans-serif +} + +body { + margin: 0; +} + +#admin-login-form { + display: grid; + width: 300px; + gap: 20px; +} + +#admin-login-form input { + padding: 4px; +} \ No newline at end of file diff --git a/default_static/admin.js b/default_static/admin.js new file mode 100644 index 0000000..065f3cd --- /dev/null +++ b/default_static/admin.js @@ -0,0 +1,10 @@ +document.getElementById('admin-login-form').onsubmit = function (e) { + e.preventDefault(); + fetch('/admin/login', { method: 'POST', body: new URLSearchParams(new FormData(e.target)) }).then(res => { + if (res.status >= 200 && res.status < 400) { + console.log(res) + window.location = '/admin/auth/workspace'; + } + + }).catch(err => console.log(err)) +} \ No newline at end of file diff --git a/default_static/script.js b/default_static/script.js index 065f3cd..d96f16a 100644 --- a/default_static/script.js +++ b/default_static/script.js @@ -1,10 +1 @@ -document.getElementById('admin-login-form').onsubmit = function (e) { - e.preventDefault(); - fetch('/admin/login', { method: 'POST', body: new URLSearchParams(new FormData(e.target)) }).then(res => { - if (res.status >= 200 && res.status < 400) { - console.log(res) - window.location = '/admin/auth/workspace'; - } - - }).catch(err => console.log(err)) -} \ No newline at end of file +console.log("Hello JS") \ No newline at end of file diff --git a/src/middleware/authentication.rs b/src/middleware/authentication.rs index 8b49854..39008a9 100644 --- a/src/middleware/authentication.rs +++ b/src/middleware/authentication.rs @@ -67,7 +67,7 @@ where if let false = authenticate(&mut req, &token).await { return Ok(req.into_response( actix_web::HttpResponse::Unauthorized() - .body("Error 401 - Unauthorized") // TODO a proper 401 view ? + .body("<html><body>Error 401 - Unauthorized - Please go to <a href='/admin/login'>login page</a>.</body></html>") // TODO a proper 401 view ? .map_into_right_body(), )); } diff --git a/src/service/admin.rs b/src/service/admin.rs index 7ed3bf0..2f7898d 100644 --- a/src/service/admin.rs +++ b/src/service/admin.rs @@ -8,6 +8,7 @@ use actix_web::{ #[get("/workspace")] async fn admin_workspace() -> impl Responder { + // TODO return admin static web view with js application actix_web::HttpResponse::Ok().body("Welcome Admin") } @@ -66,26 +67,33 @@ async fn admin_authenticate( #[get("/login")] pub async fn admin_login() -> impl Responder { - // TODO real HTML doc - create a module with admin views and a js file to load. + // TODO create a module with built-in admin static views HttpResponse::Ok().body( " -<form id='admin-login-form'> - <input type='text' name='username'/> - <input type='password' name='password' /> - <input type='submit' /> -</form> -<script> -document.getElementById('admin-login-form').onsubmit = function (e) { - e.preventDefault(); - fetch('/admin/login', { method: 'POST', body: new URLSearchParams(new FormData(e.target)) }).then(res => { - if (res.status >= 200 && res.status < 400) { - console.log(res) - window.location = '/admin/auth/workspace'; - } +<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='/static/default/admin.css'> +</head> - }).catch(err => console.log(err)) -} -</script> +<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='/static/default/admin.js'></script> +</html> ", ) } -- GitLab