Derives a type from this request using an extractor.
Returns the T extractor’s Future type which can be awaited. This is particularly handy when you want to use an extractor in a middleware implementation.
I think I would need to implement FromRequest for Login.
Ah, yes. Of course . If you want to run async code within the call method, you would do it like this:
fn call(&self, req: ServiceRequest) -> Self::Future {
Box::pin(async move {
let form_data = req.extract::<Form<Login>>().await.expect("There was no login form data in the request");
// Do something with form_data
Box::pin(async move {
let form_data = request.extract::<Form<EmployeeLogin>>().await.expect("There was no login form data in the request");
// Do something with form_data
});
But accessing request again after this call, results in the following error:
error[E0382]: use of moved value: `request`
--> src\redirect.rs:68:37
|
60 | fn call(&self, mut request: ServiceRequest) -> Self::Future {
| ----------- move occurs because `request` has type `ServiceRequest`, which does not implement the `Copy` trait
...
63 | Box::pin(async move {
| __________________-
64 | | let form_data = request.extract::<Form<Login>>().await.expect("There was no login form data in the request");
| | ------- variable moved due to use in generator
65 | | // Do something with form_data
66 | | });
| |_________- value moved here
67 |
68 | let res = self.service.call(request);
| ^^^^^^^ value used here after move
I understand what E0382 means, but I don't know how to work this case.
Thank you for all your helps -- you are very helpful, and you do point me to the right direction. My knowledge of Rust and actix-web are not up to speed to follow your suggestion as I have "new problems" ( ha ha ha ) pop up trying your suggestion.
There is actually an official actix-web middleware example on this issue, I had seen it before posting this help, it did not register and so I forgot it: