Skip to content

How do you access state in a custom Extractor? #1732

Answered by nick-kang
nick-kang asked this question in Q&A
Discussion options

You must be logged in to vote

After posting this, I think I was able to solve this, but would love to see if there is a better way.

///////////// Extractor
#[async_trait]
impl<S> FromRequestParts<S> for Authentication
where
    AppState: FromRef<S>, // <---- added this line
    S: Send + Sync,
{
    type Rejection = AppError;

    async fn from_request_parts(
        parts: &mut Parts,
        state: &S,
    ) -> std::result::Result<Self, Self::Rejection> {

        let state = parts
            .extract_with_state::<AppState, _>(state)
            .await
            .map_err(|e| AppError::InternalServerError(e.into()))?;
    }
}
#[async_trait]
impl<S> FromRequestParts<S> for AppState
where
    Self: FromRef<S>, // <-…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@davidpdrsn
Comment options

@nick-kang
Comment options

@arcstur
Comment options

@samuela
Comment options

Answer selected by davidpdrsn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants