pub struct CookieJar { /* private fields */ }
cookie
only.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) -> Self
pub fn from_headers(headers: &HeaderMap) -> Self
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 CookieJar
s through FromRequestParts
.
Sourcepub fn new() -> Self
pub fn new() -> Self
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 CookieJar
s 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: Into<Cookie<'static>>>(self, cookie: C) -> Self
pub fn remove<C: Into<Cookie<'static>>>(self, cookie: C) -> Self
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: Into<Cookie<'static>>>(self, cookie: C) -> Self
pub fn add<C: Into<Cookie<'static>>>(self, cookie: C) -> Self
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 = Infallible
type Rejection = Infallible
Source§impl IntoResponse for CookieJar
impl IntoResponse for CookieJar
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Source§impl IntoResponseParts for CookieJar
impl IntoResponseParts for CookieJar
Source§type Error = Infallible
type Error = Infallible
Source§fn into_response_parts(
self,
res: ResponseParts,
) -> Result<ResponseParts, Self::Error>
fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, Self::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 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
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 more