Skip to content

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

Open
jacobh wants to merge 2 commits intoiron:masterfrom
ArtProcessors:serde
Open

Add trait implementation for Serde structs for use as Value#14
jacobh wants to merge 2 commits intoiron:masterfrom
ArtProcessors:serde

Conversation

@jacobh
Copy link
Copy Markdown

@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