Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add how to use Arc<AppState> with the cookies examples #1560

Merged
merged 1 commit into from Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions axum-extra/src/extract/cookie/private.rs
Expand Up @@ -69,6 +69,38 @@ use std::{convert::Infallible, fmt, marker::PhantomData};
/// .route("/get", get(get_secret));
/// # let app: Router<_> = app;
/// ```
///
/// If you have been using `Arc<AppState>` you cannot implement `FromRef<Arc<AppState>> for Key`.
/// You can use a new type instead:
///
/// ```rust
/// # use axum::extract::FromRef;
/// # use axum_extra::extract::cookie::{PrivateCookieJar, Cookie, Key};
/// use std::sync::Arc;
/// use std::ops::Deref;
///
/// #[derive(Clone)]
/// struct AppState(Arc<InnerState>);
///
/// // deref so you can still access the inner fields easily
/// impl Deref for AppState {
/// type Target = InnerState;
///
/// fn deref(&self) -> &Self::Target {
/// &*self.0
/// }
/// }
///
/// struct InnerState {
/// key: Key
/// }
///
/// impl FromRef<AppState> for Key {
/// fn from_ref(state: &AppState) -> Self {
/// state.0.key.clone()
/// }
/// }
/// ```
pub struct PrivateCookieJar<K = Key> {
jar: cookie::CookieJar,
key: Key,
Expand Down
31 changes: 31 additions & 0 deletions axum-extra/src/extract/cookie/signed.rs
Expand Up @@ -87,6 +87,37 @@ use std::{convert::Infallible, fmt, marker::PhantomData};
/// .route("/me", get(me));
/// # let app: Router<_> = app;
/// ```
/// If you have been using `Arc<AppState>` you cannot implement `FromRef<Arc<AppState>> for Key`.
/// You can use a new type instead:
///
/// ```rust
/// # use axum::extract::FromRef;
/// # use axum_extra::extract::cookie::{PrivateCookieJar, Cookie, Key};
/// use std::sync::Arc;
/// use std::ops::Deref;
///
/// #[derive(Clone)]
/// struct AppState(Arc<InnerState>);
///
/// // deref so you can still access the inner fields easily
/// impl Deref for AppState {
/// type Target = InnerState;
///
/// fn deref(&self) -> &Self::Target {
/// &*self.0
/// }
/// }
///
/// struct InnerState {
/// key: Key
/// }
///
/// impl FromRef<AppState> for Key {
/// fn from_ref(state: &AppState) -> Self {
/// state.0.key.clone()
/// }
/// }
/// ```
pub struct SignedCookieJar<K = Key> {
jar: cookie::CookieJar,
key: Key,
Expand Down