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},
extract::TypedHeader,
response::{IntoResponse, Redirect},
headers::authorization::{Authorization, Bearer},
http::StatusCode,
};
use axum_extra::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
sourceimpl CookieJar
impl 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(self, cookie: Cookie<'static>) -> Self
pub fn remove(self, cookie: Cookie<'static>) -> 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::named("foo"))
}
sourcepub fn add(self, cookie: Cookie<'static>) -> Self
pub fn add(self, cookie: Cookie<'static>) -> 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
sourceimpl<B> FromRequest<B> for CookieJar where
B: Send,
impl<B> FromRequest<B> for CookieJar where
B: Send,
type Rejection = Infallible
type Rejection = Infallible
If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more
sourceimpl IntoResponse for CookieJar
impl IntoResponse for CookieJar
sourcefn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
sourceimpl IntoResponseParts for CookieJar
impl IntoResponseParts for CookieJar
type Error = Infallible
type Error = Infallible
The type returned in the event of an error. Read more
sourcefn into_response_parts(
self,
res: ResponseParts
) -> Result<ResponseParts, Self::Error>
fn into_response_parts(
self,
res: ResponseParts
) -> Result<ResponseParts, Self::Error>
Set parts of the response
Auto Trait Implementations
impl RefUnwindSafe for CookieJar
impl Send for CookieJar
impl Sync for CookieJar
impl Unpin for CookieJar
impl UnwindSafe for CookieJar
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more