pub struct CookieJar { /* private fields */ }Expand description
Extractor that grabs cookies from the request and manages the jar.
Note that methods like CookieJar::add, CookieJar::remove, etc updates the CookieJar
and returns it. This value must be returned from the handler as part of the response for the
changes to be propagated.
§Example
use axum::{
Router,
routing::{post, get},
response::{IntoResponse, Redirect},
http::StatusCode,
};
use axum_extra::{
TypedHeader,
headers::authorization::{Authorization, Bearer},
extract::cookie::{CookieJar, Cookie},
};
async fn create_session(
TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
jar: CookieJar,
) -> Result<(CookieJar, Redirect), StatusCode> {
if let Some(session_id) = authorize_and_create_session(auth.token()).await {
Ok((
// the updated jar must be returned for the changes
// to be included in the response
jar.add(Cookie::new("session_id", session_id)),
Redirect::to("/me"),
))
} else {
Err(StatusCode::UNAUTHORIZED)
}
}
async fn me(jar: CookieJar) -> Result<(), StatusCode> {
if let Some(session_id) = jar.get("session_id") {
// fetch and render user...
} else {
Err(StatusCode::UNAUTHORIZED)
}
}
async fn authorize_and_create_session(token: &str) -> Option<String> {
// authorize the user and create a session...
}
let app = Router::new()
.route("/sessions", post(create_session))
.route("/me", get(me));Implementations§
Source§impl CookieJar
impl CookieJar
Sourcepub fn from_headers(headers: &HeaderMap) -> CookieJar
pub fn from_headers(headers: &HeaderMap) -> CookieJar
Create a new CookieJar from a map of request headers.
The cookies in headers will be added to the jar.
This is intended to be used in middleware and other places where it might be difficult to
run extractors. Normally you should create CookieJars through FromRequestParts.
Sourcepub fn new() -> CookieJar
pub fn new() -> CookieJar
Create a new empty CookieJar.
This is intended to be used in middleware and other places where it might be difficult to
run extractors. Normally you should create CookieJars through FromRequestParts.
If you need a jar that contains the headers from a request use impl From<&HeaderMap> for CookieJar.
Sourcepub fn get(&self, name: &str) -> Option<&Cookie<'static>>
pub fn get(&self, name: &str) -> Option<&Cookie<'static>>
Get a cookie from the jar.
§Example
use axum_extra::extract::cookie::CookieJar;
use axum::response::IntoResponse;
async fn handle(jar: CookieJar) {
let value: Option<String> = jar
.get("foo")
.map(|cookie| cookie.value().to_owned());
}Sourcepub fn remove<C>(self, cookie: C) -> CookieJar
pub fn remove<C>(self, cookie: C) -> CookieJar
Remove a cookie from the jar.
§Example
use axum_extra::extract::cookie::{CookieJar, Cookie};
use axum::response::IntoResponse;
async fn handle(jar: CookieJar) -> CookieJar {
jar.remove(Cookie::from("foo"))
}Sourcepub fn add<C>(self, cookie: C) -> CookieJar
pub fn add<C>(self, cookie: C) -> CookieJar
Add a cookie to the jar.
The value will automatically be percent-encoded.
§Example
use axum_extra::extract::cookie::{CookieJar, Cookie};
use axum::response::IntoResponse;
async fn handle(jar: CookieJar) -> CookieJar {
jar.add(Cookie::new("foo", "bar"))
}Trait Implementations§
Source§impl<S> FromRequestParts<S> for CookieJar
impl<S> FromRequestParts<S> for CookieJar
Source§type Rejection = !
type Rejection = !
Source§fn from_request_parts<'life0, 'life1, 'async_trait>(
parts: &'life0 mut Parts,
_state: &'life1 S,
) -> Pin<Box<dyn Future<Output = Result<CookieJar, <CookieJar as FromRequestParts<S>>::Rejection>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
CookieJar: 'async_trait,
fn from_request_parts<'life0, 'life1, 'async_trait>(
parts: &'life0 mut Parts,
_state: &'life1 S,
) -> Pin<Box<dyn Future<Output = Result<CookieJar, <CookieJar as FromRequestParts<S>>::Rejection>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
CookieJar: 'async_trait,
Source§impl IntoResponse for CookieJar
impl IntoResponse for CookieJar
Source§fn into_response(self) -> Response<Body>
fn into_response(self) -> Response<Body>
Source§impl IntoResponseParts for CookieJar
impl IntoResponseParts for CookieJar
Source§fn into_response_parts(
self,
res: ResponseParts,
) -> Result<ResponseParts, <CookieJar as IntoResponseParts>::Error>
fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, <CookieJar as IntoResponseParts>::Error>
Auto Trait Implementations§
impl Freeze for CookieJar
impl RefUnwindSafe for CookieJar
impl Send for CookieJar
impl Sync for CookieJar
impl Unpin for CookieJar
impl UnsafeUnpin for CookieJar
impl UnwindSafe for CookieJar
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
tower::Layer to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Service by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Service and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
MakeService and no state. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
MakeService which stores information
about the incoming connection and has no state. Read moreimpl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more