Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trait implementation for Serde structs for use as Value #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jacobh
Copy link

@jacobh jacobh commented Nov 2, 2017

I'm using this like this...

use iron_sessionstorage;
use uuid::Uuid;

#[derive(Serialize, Deserialize)]
pub struct UserSession {
    session_id: Uuid,
}
impl UserSession {
    pub fn new_with_random_id() -> UserSession {
        UserSession {
            session_id: Uuid::new_v4(),
        }
    }
}
impl iron_sessionstorage::Key for UserSession {
    fn get_key() -> &'static str {
        "user_session"
    }
}

...

pub fn auth_post_handler(req: &mut Request) -> IronResult<Response> {
    let form = req.get::<bodyparser::Struct<AuthForm>>();
    match form {
        Ok(Some(form)) => if form.password == ADMIN_PASSWORD {
            match req.session().get::<UserSession>()? {
                None => {
                    req.session().set(UserSession::new_with_random_id())?;
                }
                _ => {}
            }
            return Ok(Response::with(status::Ok));
        } else {
            return Ok(Response::with(status::Unauthorized));
        },
        _ => return Ok(Response::with(status::BadRequest)),
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant