-
I'm doing a much-delayed upgrade to 0.6 and struggling to migrate a "session extractor" built off #[derive(Clone, FromRef)]
struct AppState {
cookie_key: Key,
client: reqwest::Client,
}
#[async_trait]
impl<S> FromRequestParts<S> for MySession
where
S: Send + Sync,
Key: FromRef<S>,
{
type Rejection = SessionAuthRedirect;
- async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
- match req.extract::<PrivateCookieJar>().await {
async fn from_request_parts(req: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
// so many options/ideas and none of them work :(
// use axum::RequestPartsExt;
// let jar = req.extract_with_state::<PrivateCookieJar, S>(state).await.unwrap();
// use axum::RequestPartsExt;
// let Extension(state) = parts.extract::<Extension<State>>()
// .await
// .map_err(|err| err.into_response())?;
// let jar: PrivateCookieJar = PrivateCookieJar::from_request_parts(req, state).await.unwrap();
match jar.get("session") {
Some(cookie) => {
let auth_string = cookie.value();
let split: Vec<&str> = auth_string.split(",").collect();
Ok(MySession {
user_id: split.get(0).unwrap().to_string(),
org_id: split.get(1).unwrap().to_string()
})
}
_ => Err(SessionAuthRedirect),
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Seems I can't mark a threaded reply as my answer, but his example here definitely got me past this (I might've had it in a random iteration previously, but it was hard to tell): (pasting only the part I stared at and used to verify my code, see thread above for the full thing) #[tokio::main]
async fn main() {
let key = Key::generate();
let app = Router::new().route("/", get(handler)).with_state(key);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
async fn handler(auth_from_parts: AuthFromParts, auth_from_request: AuthFromRequest) {}
// extractor that implements `FromRequestParts`
// implement this trait if you don't need the request body
struct AuthFromParts;
#[async_trait]
impl<S> FromRequestParts<S> for AuthFromParts
where
S: Send + Sync,
Key: FromRef<S>,
{
type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let jar = match PrivateCookieJar::<Key>::from_request_parts(parts, state).await {
Ok(jar) => jar,
// this works because `err` is `Infallible` which is an empty enum
Err(err) => match err {},
};
Ok(Self)
}
} |
Beta Was this translation helpful? Give feedback.
Seems I can't mark a threaded reply as my answer, but his example here definitely got me past this (I might've had it in a random iteration previously, but it was hard to tell):
(pasting only the part I stared at and used to verify my code, see thread above for the full thing)