#How to put the current user instance in the request after validate a jwt token in actix web

1 messages · Page 1 of 1 (latest)

flint saddle
#

Here is my call method

fn call(&self, req: ServiceRequest) -> Self::Future {

    // Do something with the request here
    let auth = req.headers().get(AUTHORIZATION);
    if auth.is_none() {
        let response = json!({
            "status": false,
            "message": "Unauthorized"
        });
        let http_res = HttpResponse::Unauthorized().json(response);
        let (http_req, _) = req.into_parts();
        let res = ServiceResponse::new(http_req, http_res);
        // Map to R type
        return (async move { Ok(res.map_into_right_body()) }).boxed_local();
    }

    let jwt_token = get_jwt_token_from_header(auth.unwrap().to_str().unwrap());


    // Clone the service to keep reference after moving into async block
    let service = self.service.clone();
   

    async move {
        // Getting some data here (just demo code for async function)
        let user = get_some_data(jwt_token).await;
        req.extensions_mut().insert(user);


        // Continue with the next middleware / handler
        let res = service.call(req).await?;
        // Map to L type
        Ok(res.map_into_left_body())
    }.boxed_local()

}

}

#

How to put the current user instance in the request after validate a jwt token in actix web

tiny oasis
#

I used to put my user in the session for future use.