diff --git a/src/admin/auth.rs b/src/admin/auth.rs index 3691a62..eef59c1 100644 --- a/src/admin/auth.rs +++ b/src/admin/auth.rs @@ -1,4 +1,4 @@ -use jsonwebtoken::EncodingKey; +use jsonwebtoken::{DecodingKey, EncodingKey}; use salvo::{ http::cookie::time::OffsetDateTime, jwt_auth::{ConstDecoder, CookieFinder, HeaderFinder, JwtAuth, QueryFinder}, @@ -40,3 +40,12 @@ pub fn jwt_middleware() -> JwtAuth { .force_passed(false); auth_handler } + +pub fn decode_token(token: &str) -> bool { + jsonwebtoken::decode::( + token, + &DecodingKey::from_secret(JWT_SECRET.as_ref()), + &jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::HS256), + ) + .is_ok() +} diff --git a/src/admin/mod.rs b/src/admin/mod.rs index 9c299c5..3b9d1de 100644 --- a/src/admin/mod.rs +++ b/src/admin/mod.rs @@ -8,11 +8,17 @@ pub mod auth; pub mod user; #[handler] -async fn login(res: &mut Response) { +async fn login(req: &mut Request, res: &mut Response) { let tera = TERA .get() .ok_or(anyhow::anyhow!("Failed to get tera")) .unwrap(); + if let Some(token) = req.cookie("token") { + if auth::decode_token(token.value()) { + res.render(Redirect::found("/article_upload")); + return; + } + } let rendered = tera .render("login.html", &Context::new()) .expect("Failed to render template"); diff --git a/src/fs_helper/md_helper.rs b/src/fs_helper/md_helper.rs index 5dd3020..3e5928c 100644 --- a/src/fs_helper/md_helper.rs +++ b/src/fs_helper/md_helper.rs @@ -108,7 +108,5 @@ mod tests { #[test] fn create_catalog_file() { let path = "assert/md/index.md"; - let result = super::add_catalog_by_upload_file(path); - assert!(result.is_ok()); } }