axum_session-0.1.0 has been yanked.

- Cookies only Store a Generated Session UUID and a Storable Boolean.
- Uses a DatabasePool Trait so you can implement your own Sub Storage Layer.
- Convenient API for
Session no need to mark as Read or Write making Usage Easier.
- Uses
dashmap for internal memory lookup and storage to achieve high throughput.
- Uses Serdes for Data Serialization so it can store any Serdes supported type's into the Sessions data.
- Supports Redis and SQLx optional Databases out of the Box.
Help
If you need help with this library or have suggestions please go to our Discord Group
Install
Axum Session uses tokio
[dependencies]
axum_session = { version = "0.1.0", features = [ "postgres-rustls"] }
Cargo Feature Flags
default: [postgres-rustls]
sqlite-rustls: Sqlx 0.6.0 support for the self-contained SQLite database engine and rustls.
sqlite-native: Sqlx 0.6.0 support for the self-contained SQLite database engine and native-tls.
postgres-rustls: Sqlx 0.6.0 support for the Postgres database server and rustls.
postgres-native: Sqlx 0.6.0 support for the Postgres database server and native-tls.
mysql-rustls: Sqlx 0.6.0 support for the MySQL/MariaDB database server and rustls.
mysql-native: Sqlx 0.6.0 support for the MySQL/MariaDB database server and native-tls.
redis-db: redis 0.21.5 session support.
Example
use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_session::{Session, SessionPgPool, SessionConfig, SessionStore, SessionLayer};
use axum::{
Router,
routing::get,
};
#[tokio::main]
async fn main() {
let poll = connect_to_database().await.unwrap();
let session_config = SessionConfig::default()
.with_table_name("test_table");
let session_store = SessionStore::<SessionPgPool>::new(Some(poll.clone().into()), session_config);
session_store.initiate().await.unwrap();
let app = Router::new()
.route("/greet", get(greet))
.layer(SessionLayer::new(session_store));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn greet(session: Session<SessionPgPool>) -> String {
let mut count: usize = session.get("count").unwrap_or(0);
count += 1;
session.set("count", count);
count.to_string()
}
async fn connect_to_database() -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
unimplemented!()
}
To enable private cookies for confidentiality, integrity, and authenticity.
When a Key is set it will automatically set the Cookie into an encypted Private cookie which
both protects the cookies data from prying eye's it also ensures the authenticity of the cookie.
Example
use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_session::{Session, SessionPgPool, SessionConfig, SessionStore, SessionLayer, SessionMode, Key};
use axum::{
Router,
routing::get,
};
#[tokio::main]
async fn main() {
let session_config = SessionConfig::default()
.with_table_name("test_table")
.with_key(Key::generate());
let session_store = SessionStore::<SessionPgPool>::new(None, session_config);
session_store.initiate().await.unwrap();
let app = Router::new()
.route("/greet", get(greet))
.layer(SessionLayer::new(session_store));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
To use axum_session in non_persistant mode Set the client to None and import SessionNullPool.
SessionNullPool is always loaded and can be used where you do not want to include any database within the build.
Example
use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_session::{Session, SessionNullPool, SessionConfig, SessionStore, SessionLayer};
use axum::{
Router,
routing::get,
};
#[tokio::main]
async fn main() {
let session_config = SessionConfig::default()
.with_table_name("test_table");
let session_store = SessionStore::<SessionNullPool>::new(None, session_config);
let app = Router::new()
.route("/greet", get(greet))
.layer(SessionLayer::new(session_store));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn greet(session: Session<SessionNullPool>) -> String {
let mut count: usize = session.get("count").unwrap_or(0);
count += 1;
session.set("count", count);
count.to_string()
}
To use axum_session with session mode set as Storable.
Example
use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
use std::net::SocketAddr;
use axum_session::{Session, SessionPgPool, SessionConfig, SessionStore, SessionLayer, SessionMode};
use axum::{
Router,
routing::get,
};
#[tokio::main]
async fn main() {
let session_config = SessionConfig::default()
.with_table_name("test_table").with_mode(SessionMode::Storable);
let session_store = SessionStore::<SessionPgPool>::new(None, session_config);
session_store.initiate().await.unwrap();
let app = Router::new()
.route("/greet", get(greet))
.layer(SessionLayer::new(session_store));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn greet(session: Session<SessionPgPool>) -> String {
let mut count: usize = session.get("count").unwrap_or(0);
session.set_store(true);
count += 1;
session.set("count", count);
count.to_string()
}
Session Login and Authentication via axum_sessions_auth
For user login, login caching and authentication please see axum_sessions_auth.