Skip to content

Commit

Permalink
update login redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
secheng722 committed May 20, 2024
1 parent 4c76d80 commit 76f8c4c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/admin/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use jsonwebtoken::EncodingKey;
use jsonwebtoken::{DecodingKey, EncodingKey};
use salvo::{
http::cookie::time::OffsetDateTime,
jwt_auth::{ConstDecoder, CookieFinder, HeaderFinder, JwtAuth, QueryFinder},
Expand Down Expand Up @@ -40,3 +40,12 @@ pub fn jwt_middleware() -> JwtAuth<JwtClaims, ConstDecoder> {
.force_passed(false);
auth_handler
}

pub fn decode_token(token: &str) -> bool {
jsonwebtoken::decode::<JwtClaims>(
token,
&DecodingKey::from_secret(JWT_SECRET.as_ref()),
&jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::HS256),
)
.is_ok()
}
8 changes: 7 additions & 1 deletion src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 0 additions & 2 deletions src/fs_helper/md_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 76f8c4c

Please sign in to comment.