Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make middleware support async fucntion call? #40

Open
measproem opened this issue Sep 14, 2022 · 2 comments
Open

How to make middleware support async fucntion call? #40

measproem opened this issue Sep 14, 2022 · 2 comments

Comments

@measproem
Copy link

Your project very good skeleton of Actix Web rest API especially your Middleware jwt concept , I have changed to support other database library beside diesel , every thing work fine but I have one issue because of new database library need to working on async example token_utils::verify_token(&token_data, pool).await in file name auth_middleware.rs I hard to call it or how make it works in middleware.
I hope get your help
Thank you

@measproem measproem changed the title With middleware function token_utils::verify_token how to support aysnc How to make middleware support async fucntion call? Sep 14, 2022
@FrostPrice
Copy link

Hello.
I was able to solve this middleware with async functions by doing the following:

Inside the Impl for the AuthenticationMiddleware<S> in the line where I need to make an async call to the DB, I used the executor::block_on() method, and created async block that will have the call of the async function with .await on it.

let my_awaited_return = executor::block_on( async {
      my_async_function(&variable).await
});

Depending on your necessity, you might need to move the values to the async block, like this:

let my_awaited_return = executor::block_on( async move {
      my_async_function(&moved_variable).await
});

This also worked with actix-web version 4

I hope it helps.

Best Regards.

@measproem
Copy link
Author

Thank you

@github-staff github-staff deleted a comment from apocanow1 Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants