Browse Source

Move auth checking to middleware

master
Dylan Baker 3 years ago
parent
commit
7062a29ac8
3 changed files with 38 additions and 27 deletions
  1. 9
    3
      src/main.rs
  2. 16
    2
      src/middleware.rs
  3. 13
    22
      src/routes.rs

+ 9
- 3
src/main.rs View File

@@ -53,11 +53,17 @@ async fn main() -> std::io::Result<()> {
53 53
     app.with(middleware::session());
54 54
 
55 55
     app.at("/").get(routes::index);
56
-    app.at("/posts").post(routes::create_post);
56
+    app.at("/posts")
57
+        .with(middleware::require_auth)
58
+        .post(routes::create_post);
57 59
     app.at("/posts/:id").get(routes::single_post);
58
-    app.at("/posts/:id/edit").get(routes::edit_post);
60
+    app.at("/posts/:id/edit")
61
+        .with(middleware::require_auth)
62
+        .get(routes::edit_post);
59 63
     app.at("/login").get(routes::login_page).post(routes::login);
60
-    app.at("/logout").post(routes::logout);
64
+    app.at("/logout")
65
+        .with(middleware::require_auth)
66
+        .post(routes::logout);
61 67
 
62 68
     app.listen("127.0.0.1:8080").await?;
63 69
 

+ 16
- 2
src/middleware.rs View File

@@ -1,10 +1,10 @@
1 1
 use tide::{
2 2
     http::mime,
3 3
     sessions::{CookieStore, SessionMiddleware},
4
-    Response, Result, StatusCode,
4
+    Next, Redirect, Request, Response, Result, StatusCode,
5 5
 };
6 6
 
7
-use std::{env, io::ErrorKind};
7
+use std::{env, future::Future, io::ErrorKind, pin::Pin};
8 8
 use tera::Context;
9 9
 
10 10
 use crate::routes;
@@ -21,6 +21,20 @@ pub fn session() -> SessionMiddleware<CookieStore> {
21 21
     )
22 22
 }
23 23
 
24
+pub fn require_auth<'a>(
25
+    req: Request<()>,
26
+    next: Next<'a, ()>,
27
+) -> Pin<Box<dyn Future<Output = Result> + 'a + Send>> {
28
+    Box::pin(async {
29
+        let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
30
+        if logged_in {
31
+            Ok(next.run(req).await)
32
+        } else {
33
+            Ok(Redirect::new("/login").into())
34
+        }
35
+    })
36
+}
37
+
24 38
 pub async fn errors(mut res: Response) -> Result<Response> {
25 39
     let mut context = Context::new();
26 40
 

+ 13
- 22
src/routes.rs View File

@@ -25,17 +25,12 @@ pub async fn index(req: Request<()>) -> Result {
25 25
 }
26 26
 
27 27
 pub async fn create_post(mut req: Request<()>) -> Result {
28
-    let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
29
-    if !logged_in {
30
-        Ok(tide::Redirect::new("/login").into())
31
-    } else {
32
-        let mut post: Post = req.body_form().await?;
33
-        post.id = Uuid::new_v4().to_string();
34
-        post.date = Local::now().date().naive_local().to_string();
35
-        post.body = post.body.trim().to_owned();
36
-        post.save().await?;
37
-        Ok(tide::Redirect::new("/").into())
38
-    }
28
+    let mut post: Post = req.body_form().await?;
29
+    post.id = Uuid::new_v4().to_string();
30
+    post.date = Local::now().date().naive_local().to_string();
31
+    post.body = post.body.trim().to_owned();
32
+    post.save().await?;
33
+    Ok(tide::Redirect::new("/").into())
39 34
 }
40 35
 
41 36
 pub async fn single_post(req: Request<()>) -> Result {
@@ -50,17 +45,13 @@ pub async fn single_post(req: Request<()>) -> Result {
50 45
 
51 46
 pub async fn edit_post(req: Request<()>) -> Result {
52 47
     let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
53
-    if !logged_in {
54
-        Ok(tide::Redirect::new("/login").into())
55
-    } else {
56
-        let mut context = Context::new();
57
-        context.insert("logged_in", &logged_in);
58
-        let post_id = req.param("id")?;
59
-        let mut post = fs::get_one_post(post_id).await?;
60
-        post.body = post.body.replace("<br>", "\n");
61
-        context.insert("post", &post);
62
-        render_response("edit.html", &context)
63
-    }
48
+    let mut context = Context::new();
49
+    context.insert("logged_in", &logged_in);
50
+    let post_id = req.param("id")?;
51
+    let mut post = fs::get_one_post(post_id).await?;
52
+    post.body = post.body.replace("<br>", "\n");
53
+    context.insert("post", &post);
54
+    render_response("edit.html", &context)
64 55
 }
65 56
 
66 57
 pub async fn login_page(mut req: Request<()>) -> Result {

Loading…
Cancel
Save