Crates.io | rocket-session-store |
lib.rs | rocket-session-store |
version | 0.2.0 |
source | src |
created_at | 2022-02-05 21:24:38.467753 |
updated_at | 2022-04-01 20:59:37.631321 |
description | A rust session library that can use a custom store. |
homepage | https://github.com/Aurora2500/rocket-session-store |
repository | https://github.com/Aurora2500/rocket-session-store |
max_upload_size | |
id | 527587 |
size | 23,006 |
rocket-session-store is a library for the rocket web framework. It manages sessions by using cookies and a customizable store.
Using this library consists of two steps:
use rocket_session_store::{
memory::MemoryStore,
SessionStore,
SessionResult,
Session,
CookieConfig,
};
use rocket::{
Rocket,
get,
routes,
launch,
};
use std::time::Duration;
// Using the `Session` request guard
#[get("/")]
async fn index(session: Session<'_, String>) -> SessionResult<String> {
let name: Option<String> = session.get().await?;
if let Some(name) = name {
Ok(format!("Hello, {}!", name))
} else {
Ok("Hello, world!".into())
}
}
#[launch]
fn rocket() -> _ {
// Instance a store that fits your needs and wrap it in a Box in SessionStore.
let memory_store: MemoryStore::<String> = MemoryStore::default();
let store: SessionStore<String> = SessionStore {
store: Box::new(memory_store),
name: "token".into(),
duration: Duration::from_secs(3600 * 24 * 3),
// The cookie config is used to set the cookie's path and other options.
cookie: CookieConfig::default(),
};
// Attach it to a rocket by calling `fairing()`
rocket::build().attach(store.fairing()).mount("/", routes![index])
}
If you wish to contribute, please read CONTRIBUTING.md.