|
1 |
| -> [!IMPORTANT] |
2 |
| -> **Migrating to [`tower-sessions`](https://github.com/maxcountryman/tower-sessions)** |
| 1 | +> [!IMPORTANT] > **Migration to `tower-sessions`** |
3 | 2 | >
|
4 |
| -> We have moved development of this crate to `tower-sessions`. |
| 3 | +> **Development of this crate has moved to [`tower-sessions`](https://github.com/maxcountryman/tower-sessions).** Please consider migrating. |
5 | 4 | >
|
6 |
| -> Please **consider migrating** to `tower-sessions` if you use this crate. |
7 |
| -
|
8 |
| -<h1 align="center"> |
9 |
| -axum-sessions |
10 |
| -</h1> |
11 |
| - |
12 |
| -<p align="center"> |
13 |
| -🥠 Cookie-based sessions for Axum via async-session. |
14 |
| -</p> |
15 |
| - |
16 |
| -<div align="center"> |
17 |
| -<a href="https://crates.io/crates/axum-sessions"> |
18 |
| -<img src="https://img.shields.io/crates/v/axum-sessions.svg" /> |
19 |
| -</a> |
20 |
| -<a href="https://docs.rs/axum-sessions"> |
21 |
| -<img src="https://docs.rs/axum-sessions/badge.svg" /> |
22 |
| -</a> |
23 |
| -<a href="https://github.com/maxcountryman/axum-sessions/actions/workflows/rust.yml"> |
24 |
| -<img src="https://github.com/maxcountryman/axum-sessions/actions/workflows/rust.yml/badge.svg" /> |
25 |
| -</a> |
26 |
| -<a href='https://coveralls.io/github/maxcountryman/axum-sessions?branch=main'> |
27 |
| -<img src='https://coveralls.io/repos/github/maxcountryman/axum-sessions/badge.svg?branch=main' alt='Coverage Status' /> |
28 |
| -</a> |
29 |
| -</div> |
30 |
| - |
31 |
| -## 🎨 Overview |
32 |
| - |
33 |
| -`axum-sessions` is a middleware providing cookie-based sessions for `axum` applications. |
34 |
| - |
35 |
| -- Cryptographically-signed cookies, ensuring integrity and authenticity |
36 |
| -- Wraps `async-session`, enabling flexible cookie storage (e.g. `async-sqlx-session`) |
37 |
| -- Convenient extractor-based API (i.e. `ReadableSession` and `WritableSession`) |
38 |
| -- Can be used as a generic Tower middleware |
39 |
| - |
40 |
| -## 📦 Install |
41 |
| - |
42 |
| -To use the crate in your project, add the following to your `Cargo.toml` file: |
43 |
| - |
44 |
| -```toml |
45 |
| -[dependencies] |
46 |
| -axum-sessions = "0.5.0" |
47 |
| -``` |
48 |
| - |
49 |
| -## 🤸 Usage |
50 |
| - |
51 |
| -`axum` applications can use the middleware via the session layer. |
52 |
| - |
53 |
| -### `axum` Example |
54 |
| - |
55 |
| -```rust |
56 |
| -use axum::{response::IntoResponse, routing::get, Router}; |
57 |
| -use axum_sessions::{ |
58 |
| - async_session::MemoryStore, |
59 |
| - extractors::{ReadableSession, WritableSession}, |
60 |
| - SessionLayer, |
61 |
| -}; |
62 |
| -use rand::Rng; |
63 |
| - |
64 |
| -#[tokio::main] |
65 |
| -async fn main() { |
66 |
| - let store = MemoryStore::new(); |
67 |
| - let secret = rand::thread_rng().gen::<[u8; 128]>(); |
68 |
| - let session_layer = SessionLayer::new(store, &secret).with_secure(false); |
69 |
| - |
70 |
| - async fn display_handler(session: ReadableSession) -> impl IntoResponse { |
71 |
| - let mut count = 0; |
72 |
| - count = session.get("count").unwrap_or(count); |
73 |
| - format!( |
74 |
| - "Count is: {}; visit /inc to increment and /reset to reset", |
75 |
| - count |
76 |
| - ) |
77 |
| - } |
78 |
| - |
79 |
| - async fn increment_handler(mut session: WritableSession) -> impl IntoResponse { |
80 |
| - let mut count = 1; |
81 |
| - count = session.get("count").map(|n: i32| n + 1).unwrap_or(count); |
82 |
| - session.insert("count", count).unwrap(); |
83 |
| - format!("Count is: {}", count) |
84 |
| - } |
85 |
| - |
86 |
| - async fn reset_handler(mut session: WritableSession) -> impl IntoResponse { |
87 |
| - session.destroy(); |
88 |
| - "Count reset" |
89 |
| - } |
90 |
| - |
91 |
| - let app = Router::new() |
92 |
| - .route("/", get(display_handler)) |
93 |
| - .route("/inc", get(increment_handler)) |
94 |
| - .route("/reset", get(reset_handler)) |
95 |
| - .layer(session_layer); |
96 |
| - |
97 |
| - axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) |
98 |
| - .serve(app.into_make_service()) |
99 |
| - .await |
100 |
| - .unwrap(); |
101 |
| -} |
102 |
| -``` |
103 |
| - |
104 |
| -You can find this [example][counter-example] as well as other example projects in the [example directory][examples]. |
105 |
| - |
106 |
| -## Session authentication via `axum-login` |
107 |
| - |
108 |
| -For user session management and authentication and authorization specifically please see [`axum-login`](https://github.com/maxcountryman/axum-login). |
109 |
| - |
110 |
| -See the [crate documentation][docs] for more usage information. |
111 |
| - |
112 |
| -[counter-example]: https://github.com/maxcountryman/axum-sessions/tree/main/examples/counter |
113 |
| -[examples]: https://github.com/maxcountryman/axum-sessions/tree/main/examples |
114 |
| -[docs]: https://docs.rs/axum-sessions |
| 5 | +> Numerous bugs and a significant design flaw with `axum-sessions` are addressed with `tower-sessions`. |
0 commit comments