| Crates.io | rocket_flex_session |
| lib.rs | rocket_flex_session |
| version | 0.2.0 |
| created_at | 2025-09-03 04:59:01.824507+00 |
| updated_at | 2025-09-12 03:57:53.746745+00 |
| description | Flexible sessions for Rocket applications |
| homepage | https://github.com/fa-sharp/rocket-flex-session |
| repository | https://github.com/fa-sharp/rocket-flex-session |
| max_upload_size | |
| id | 1822062 |
| size | 251,370 |
Simple, extensible session library for Rocket applications.
Add to your Cargo.toml:
[dependencies]
rocket = "0.5"
rocket-flex-session = { version = "0.2" }
Basic usage:
use rocket::routes;
use rocket_flex_session::{Session, RocketFlexSession};
#[derive(Clone)]
struct MySession {
user_id: String,
}
#[rocket::launch]
fn rocket() -> _ {
rocket::build()
.attach(RocketFlexSession::<MySession>::default())
.mount("/", routes![login])
}
#[rocket::post("/login")]
async fn login(mut session: Session<MySession>) {
session.set(MySession { user_id: "123".to_owned() });
}
Memory (default) - In-memory storage, for local development
Cookie - Client-side encrypted cookies, serialized using serde (cookie feature)
Redis - Redis-backed sessions via the fred crate (redis_fred feature)
SQL Database - Postgres and SQLite-backed sessions via sqlx (sqlx_postgres and sqlx_sqlite features)
Custom - Custom storage possible by implementing the SessionStorage trait
Build authentication and authorization layers using Rocket's request guard system:
#[rocket::async_trait]
impl<'r> FromRequest<'r> for MySession {
type Error = &'r str;
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let session = req.guard::<Session<MySession>>().await.expect("should not fail");
match session.get() {
Some(data) => Outcome::Success(data),
None => Outcome::Error((Status::Unauthorized, "Not logged in")),
}
}
}
See the full documentation for detailed usage examples, configuration options, and common patterns.
This project is licensed under the MIT license.