1
0
mirror of https://github.com/actix/examples synced 2024-12-03 18:22:14 +01:00
examples/middleware/src/redirect.rs

29 lines
811 B
Rust
Raw Normal View History

2018-07-09 21:36:03 +02:00
extern crate actix_web;
use actix_web::middleware::{Middleware, Started};
use actix_web::{http, HttpRequest, HttpResponse, Result};
pub struct CheckLogin;
impl<S> Middleware<S> for CheckLogin {
// We only need to hook into the `start` for this middleware.
2018-07-16 08:36:53 +02:00
fn start(&self, req: &HttpRequest<S>) -> Result<Started> {
2018-07-09 21:36:03 +02:00
let is_logged_in = false; // Change this to see the change in outcome in the browser
if is_logged_in {
return Ok(Started::Done);
}
// Don't forward to /login if we are already on /login
if req.path() == "/login" {
return Ok(Started::Done);
}
Ok(Started::Response(
HttpResponse::Found()
.header(http::header::LOCATION, "/login")
.finish(),
))
}
}