Trait fe_session::FeSessionRequest [] [src]

pub trait FeSessionRequest {
    fn create_session<T: Any>(&mut self, session: T) -> FeSession<T>;
    fn clear_session<T: Any>(&mut self);
    fn get_session<T: Any>(&self) -> FeSession<T>;
}

Helper trait that adds methods for accessing a user's session onto the Iron's Request object.

This trait provides the main interface for your handler to interact with the user's sessions.

Examples

use iron::{Iron, Chain, Request, Response};
use iron::status::Status;
use fe_session::{FeSessionStorage, FeSessionRequest, FeConnection};

struct MySession {
    name : String
}

let mut chain = Chain::new(|req : &mut Request| {
    if req.url.path()[0] == "login" {
        req.create_session(MySession { name : "john".to_string() }); // create the session
        Ok(Response::with((Status::Ok, "hello")))
    }
    else if req.url.path()[0] == "view" {
        let mut access = req.get_session::<MySession>();  // Access the session
        let session_guard = access.get();

        Ok(Response::with((Status::Ok, session_guard.name.clone())))
    }
    else if req.url.path()[0] == "logout" {
        req.clear_session::<MySession>(); // Delete the session
        Ok(Response::with((Status::Ok, "bye")))
    }
    else {
        Ok(Response::with(Status::NotFound))
    }
});
chain.around(FeSessionStorage::<MySession>::new());

let mut iron = Iron::new(chain).http(("localhost", 3000)).unwrap();

let mut conn = FeConnection::new("localhost", 3000);

assert_eq!(conn.get_string("/login"), "hello");
assert_eq!(conn.get_string("/view"), "john");
assert_eq!(conn.get_string("/logout"), "bye");

iron.close().unwrap();

Required Methods

fn create_session<T: Any>(&mut self, session: T) -> FeSession<T>

Creates a new session on the request, removing the previous session if it exists.

The client will also get a new session ID. Any requests that come in with the old session ID will have a state of of "Expired". This causes a set-cookie header to be included in the response so the client knows what cookie to send back in the future for this session.

session - The custom session to use for this client.

Returns the newly created FeSession object with a last access time of now.

Examples

use iron::{Iron, Chain, Request, Response};
use iron::status::Status;
use fe_session::{FeSessionStorage, FeSessionRequest, FeSessionState, FeConnection};

struct MySession;

let mut chain = Chain::new(|req : &mut Request| {
    if req.url.path()[0] == "login" {
        req.create_session(MySession); // create the session
        Ok(Response::with((Status::Ok, "created")))
    }
    else if req.url.path()[0] == "status" {
        let mut access = req.get_session::<MySession>();

        let state = match access.state() {
            FeSessionState::Active => "active",
            FeSessionState::Expired => "expired",
            FeSessionState::None => "none"
        };

        Ok(Response::with((Status::Ok, state)))
    }
    else {
        Ok(Response::with(Status::NotFound))
    }
});
chain.around(FeSessionStorage::<MySession>::new());

let mut iron = Iron::new(chain).http(("localhost", 3000)).unwrap();

let mut conn = FeConnection::new("localhost", 3000);

assert_eq!(conn.get_string("/status"), "none");
assert_eq!(conn.get_string("/login"), "created");
assert_eq!(conn.get_string("/status"), "active");

iron.close().unwrap();

fn clear_session<T: Any>(&mut self)

Clears out the session from the request.

If the session is currently active it will be deleted from memory. The session cookie will also be cleared. If the session is not currently active this method will not harm anything.

Examples

use iron::{Iron, Chain, Request, Response};
use iron::status::Status;
use fe_session::{FeSessionStorage, FeSessionRequest, FeSessionState, FeConnection};

struct MySession;

let mut chain = Chain::new(|req : &mut Request| {
    if req.url.path()[0] == "login" {
        req.create_session(MySession); // create the session
        Ok(Response::with((Status::Ok, "hello")))
    }
    else if req.url.path()[0] == "status" {
        let mut access = req.get_session::<MySession>();

        let state = match access.state() {
            FeSessionState::Active => "active",
            FeSessionState::Expired => "expired",
            FeSessionState::None => "none"
        };

        Ok(Response::with((Status::Ok, state)))
    }
    else if req.url.path()[0] == "logout" {
        req.clear_session::<MySession>(); // Delete the session
        Ok(Response::with((Status::Ok, "bye")))
    }
    else {
        Ok(Response::with(Status::NotFound))
    }
});
chain.around(FeSessionStorage::<MySession>::new());

let mut iron = Iron::new(chain).http(("localhost", 3000)).unwrap();

let mut conn = FeConnection::new("localhost", 3000);

assert_eq!(conn.get_string("/login"), "hello");
assert_eq!(conn.get_string("/status"), "active");
assert_eq!(conn.get_string("/logout"), "bye");
assert_eq!(conn.get_string("/status"), "none");

iron.close().unwrap();

fn get_session<T: Any>(&self) -> FeSession<T>

Returns the current session for the client.

Examples

use iron::{Iron, Chain, Request, Response};
use iron::status::Status;
use fe_session::{FeSessionStorage, FeSessionRequest, FeConnection};

struct MySession {
    name : String
}

let mut chain = Chain::new(|req : &mut Request| {
    if req.url.path()[0] == "login" {
        req.create_session(MySession { name : "john".to_string() }); // create the session
        Ok(Response::with((Status::Ok, "hello")))
    }
    else if req.url.path()[0] == "view" {
        let mut access = req.get_session::<MySession>();  // Access the session
        let session_guard = access.get();

        Ok(Response::with((Status::Ok, session_guard.name.clone())))
    }
    else if req.url.path()[0] == "logout" {
        req.clear_session::<MySession>(); // Delete the session
        Ok(Response::with((Status::Ok, "bye")))
    }
    else {
        Ok(Response::with(Status::NotFound))
    }
});
chain.around(FeSessionStorage::<MySession>::new());

let mut iron = Iron::new(chain).http(("localhost", 3000)).unwrap();

let mut conn = FeConnection::new("localhost", 3000);

assert_eq!(conn.get_string("/login"), "hello");
assert_eq!(conn.get_string("/view"), "john");
assert_eq!(conn.get_string("/logout"), "bye");

iron.close().unwrap();

Panics

If SessionStorage was not included in the iron chain.

Implementors