Skip to content

Access session in BeforeMiddleware panics #6

@frjonsen

Description

@frjonsen

Hi. I'm trying to access the session in a BeforeMiddleware, in order to check if the request is authenticated. This is my middleware:

struct SessionMiddleware<C: Connection> {
    connection: Arc<C>
}

impl typemap::Key for User {
    type Value = User;
}

impl<C: Connection + 'static> BeforeMiddleware for SessionMiddleware<C> {

    fn before(&self, req: &mut Request) -> IronResult<()> {
        use ::entities::Session;
        use iron_sessionstorage::{SessionRequestExt,Value};
        if let Some(session) = try!(req.session().get::<Session>()) {
            let sess: String = session.into_raw();
            let connection = self.connection.clone();
            let valid_seconds = ::CONFIG.read().unwrap().get_int("sessions.duration");
            if let Ok(Some(user)) = connection.verify_session(&sess, valid_seconds) {
            req.extensions.insert::<User>(user);
            }
        }
        Ok(())
    }
}

This is causing a panic here: https://github.com/iron/iron-sessionstorage/blob/master/src/lib.rs#L131

Is there any way to do what I'm attempting to do, or will I have to move this to the Handlers? I'd rather not put it in the Handlers, since I want to do this for every request, which seems to be pretty much the entire point of BeforeMiddlewares. I suspect that the fact that sessionstorage is an AroundMiddleware makes it impossible to access Sessions in a BeforeMiddleware?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions