2019-12-07 18:59:24 +01:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-03-11 03:19:50 +01:00
|
|
|
use actix_service::{Service, Transform};
|
|
|
|
use actix_web::dev::{ServiceRequest, ServiceResponse};
|
2019-04-25 20:19:21 +02:00
|
|
|
use actix_web::{http, Error, HttpResponse};
|
2019-12-07 18:59:24 +01:00
|
|
|
use futures::future::{ok, Either, Ready};
|
2018-07-09 21:36:03 +02:00
|
|
|
|
|
|
|
pub struct CheckLogin;
|
|
|
|
|
2019-04-14 19:34:41 +02:00
|
|
|
impl<S, B> Transform<S> for CheckLogin
|
2019-03-11 03:19:50 +01:00
|
|
|
where
|
2019-04-25 20:19:21 +02:00
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
2019-03-11 03:19:50 +01:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
2019-04-14 19:34:41 +02:00
|
|
|
type Request = ServiceRequest;
|
2019-03-11 03:19:50 +01:00
|
|
|
type Response = ServiceResponse<B>;
|
2019-04-25 20:19:21 +02:00
|
|
|
type Error = Error;
|
2019-03-11 03:19:50 +01:00
|
|
|
type InitError = ();
|
|
|
|
type Transform = CheckLoginMiddleware<S>;
|
2019-12-07 18:59:24 +01:00
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
2019-03-11 03:19:50 +01:00
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
|
|
|
ok(CheckLoginMiddleware { service })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub struct CheckLoginMiddleware<S> {
|
|
|
|
service: S,
|
|
|
|
}
|
|
|
|
|
2019-04-14 19:34:41 +02:00
|
|
|
impl<S, B> Service for CheckLoginMiddleware<S>
|
2019-03-11 03:19:50 +01:00
|
|
|
where
|
2019-04-25 20:19:21 +02:00
|
|
|
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
2019-03-11 03:19:50 +01:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
2019-04-14 19:34:41 +02:00
|
|
|
type Request = ServiceRequest;
|
2019-03-11 03:19:50 +01:00
|
|
|
type Response = ServiceResponse<B>;
|
2019-04-25 20:19:21 +02:00
|
|
|
type Error = Error;
|
2019-12-07 18:59:24 +01:00
|
|
|
type Future = Either<S::Future, Ready<Result<Self::Response, Self::Error>>>;
|
2019-03-11 03:19:50 +01:00
|
|
|
|
2019-12-07 18:59:24 +01:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(cx)
|
2019-03-11 03:19:50 +01:00
|
|
|
}
|
|
|
|
|
2019-04-14 19:34:41 +02:00
|
|
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
2019-03-11 03:19:50 +01:00
|
|
|
// We only need to hook into the `start` for this middleware.
|
|
|
|
|
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 {
|
2019-12-07 18:59:24 +01:00
|
|
|
Either::Left(self.service.call(req))
|
2019-03-11 03:19:50 +01:00
|
|
|
} else {
|
|
|
|
// Don't forward to /login if we are already on /login
|
|
|
|
if req.path() == "/login" {
|
2019-12-07 18:59:24 +01:00
|
|
|
Either::Left(self.service.call(req))
|
2019-03-11 03:19:50 +01:00
|
|
|
} else {
|
2019-12-07 18:59:24 +01:00
|
|
|
Either::Right(ok(req.into_response(
|
2019-03-11 03:19:50 +01:00
|
|
|
HttpResponse::Found()
|
|
|
|
.header(http::header::LOCATION, "/login")
|
|
|
|
.finish()
|
|
|
|
.into_body(),
|
|
|
|
)))
|
|
|
|
}
|
2018-07-09 21:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|