sakadream / actix-web-rest-api-with-jwt Goto Github PK
View Code? Open in Web Editor NEWA simple CRUD backend app using Actix-web, Diesel and JWT
License: MIT License
A simple CRUD backend app using Actix-web, Diesel and JWT
License: MIT License
Hi,
First of all I would like to say that this repository is a really nice example of implementing JWT for Actix. It is a really helpful resource to start playing with Actix, thank you for your work.
If you have a minute to spare would you be able to suggest how to pass JWT token to the different controllers? What I would like to achieve is to have a way of extracting user_id from incoming request to automatically create relation between models and user that is creating them in the app, without a need to send user_id with every request from frontend.
If you don't want to or do not have time to, feel free to close this issue :)
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
thread 'main' panicked at 'System is not running', /Users/nick/.cargo/registry/src/mirrors.ustc.edu.cn-15f9db60536bad60/actix-rt-1.1.1/src/system.rs:78:21
note: run with RUST_BACKTRACE=1
environment variable to display a backtrace
sudo apt-get install libsqlite3-dev libpq-dev
Step 10/19 : RUN rm ./target/release/deps/actix_web_rest_api_with_jwt*; cargo build --release
---> Running in 2c13bce0bc9a
rm: cannot remove './target/release/deps/actix_web_rest_api_with_jwt*': No such file or directory
Compiling migrations_macros v1.4.2
Compiling actix-connect v2.0.0
Compiling actix-http v2.1.0
error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.0.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.1.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.10.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.11.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.12.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.13.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.14.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.15.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.2.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.3.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.4.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.5.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.6.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.7.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.8.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.migrations_macros.8uzsnkrf-cgu.9.rcgu.o" "-o" "/app/target/release/deps/libmigrations_macros-fadaadeab581f780.so" "/app/target/release/deps/migrations_macros-fadaadeab581f780.4a7fc84kdbhygpn1.rcgu.o" "/app/target/release/deps/migrations_macros-fadaadeab581f780.39u2d4eyt0em7h1g.rcgu.o" "-Wl,--gc-sections" "-shared" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/app/target/release/deps" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/app/target/release/deps/libsyn-49d5112fbab80ab7.rlib" "/app/target/release/deps/libquote-2be1e76992997c83.rlib" "/app/target/release/deps/libproc_macro2-c85bd876b72519c4.rlib" "/app/target/release/deps/libunicode_xid-8663f59690402b5f.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libproc_macro-8975dabc5e7e4cfc.rlib" "/app/target/release/deps/libmigrations_internals-86d9a50d9b9eb1a1.rlib" "/app/target/release/deps/libdiesel-31f8b466bfad945b.rlib" "/app/target/release/deps/liblibsqlite3_sys-f9bc70751cec2dd6.rlib" "/app/target/release/deps/libpq_sys-3f41eabf954f0f9e.rlib" "/app/target/release/deps/libchrono-1440cb9e8b9ec093.rlib" "/app/target/release/deps/libserde-30b28169d4953485.rlib" "/app/target/release/deps/libnum_integer-954c7c5dc74de8b2.rlib" "/app/target/release/deps/libnum_traits-bac497dc21ff48bd.rlib" "/app/target/release/deps/libtime-5656809522972b8c.rlib" "/app/target/release/deps/libr2d2-1b2d10203b26c854.rlib" "/app/target/release/deps/libscheduled_thread_pool-b391843d611afae2.rlib" "/app/target/release/deps/libparking_lot-0ba472e40b20f1ad.rlib" "/app/target/release/deps/libparking_lot_core-fc1917ca1b019cec.rlib" "/app/target/release/deps/liblibc-55d7ced54f21c4a3.rlib" "/app/target/release/deps/libsmallvec-8ed215ffb5a01ac4.rlib" "/app/target/release/deps/liblock_api-dc555b5792d32d68.rlib" "/app/target/release/deps/libscopeguard-7960dfc10554893d.rlib" "/app/target/release/deps/libinstant-322f0e9f791896ac.rlib" "/app/target/release/deps/libcfg_if-e87b58d2de662d30.rlib" "/app/target/release/deps/liblog-ad859a5ddc9be878.rlib" "/app/target/release/deps/libcfg_if-3b1df0a1ef4b649d.rlib" "/app/target/release/deps/libbyteorder-655becf0e7bd92aa.rlib" "/app/target/release/deps/libbitflags-02f6fb34a765e434.rlib" "-Wl,--start-group" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-f14aca24435a5414.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-48d342a8b48d1d01.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-14bc0820888c8eb3.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-9cbd9e217bff06bc.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-31826136df98934e.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-075976a117c8fd5d.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-2d5cbedfbf17a011.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-0474372ff08c5319.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-d437c34460d2315a.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-fb61ed1b8cc4de79.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-bf76d1b643bfc9f0.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-a1b53aa7fddcf418.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-28585e57fac45c73.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-64801769bc15ab28.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-541997b56bb98660.rlib" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-cdea3c81adab3d12.rlib" "-Wl,--end-group" "/usr/local/rustup/toolchains/1.47.0-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-cd9f15a39fb65cbc.rlib" "-Wl,-Bdynamic" "-lsqlite3" "-lpq" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-ldl" "-lutil"
= note: /usr/bin/ld: cannot find -lsqlite3
collect2: error: ld returned 1 exit status
error: aborting due to previous error
error: could not compile `migrations_macros`.
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
ERROR: Service 'app' failed to build : The command '/bin/sh -c rm ./target/release/deps/actix_web_rest_api_with_jwt*; cargo build --release' returned a non-zero code: 101
I there any easy way to update it to actix-web 4?)
I got few error during the build:
error[E0432]: unresolved imports `actix_web::http::HeaderName`, `actix_web::http::HeaderValue`
--> src/middleware/auth_middleware.rs:5:12
|
5 | http::{HeaderName, HeaderValue, Method},
| ^^^^^^^^^^ ^^^^^^^^^^^ no `HeaderValue` in `http`
| |
| no `HeaderName` in `http`
error[E0277]: the trait bound `fn() -> HttpResponse {ping}: Handler<_>` is not satisfied
--> src/api/ping_controller.rs:4:4
|
3 | #[get("/ping")]
| --------------- required by a bound introduced by this call
4 | fn ping() -> HttpResponse {
| ^^^^ the trait `Handler<_>` is not implemented for `fn() -> HttpResponse {ping}`
|
note: required by a bound in `Resource::<T>::to`
--> /Users/nshv/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.2.1/src/resource.rs:224:12
|
224 | F: Handler<Args>,
| ^^^^^^^^^^^^^ required by this bound in `Resource::<T>::to`
error[E0277]: the trait bound `actix_web::dev::Response<_>: std::convert::From<BoxBody>` is not satisfied
--> src/middleware/auth_middleware.rs:109:21
|
108 | Ok(req.into_response(
| ------------- required by a bound introduced by this call
109 | / HttpResponse::Unauthorized()
110 | | .json(ResponseBody::new(
111 | | constants::MESSAGE_INVALID_TOKEN,
112 | | constants::EMPTY,
113 | | ))
114 | | .into_body(),
| |____________________________________^ the trait `std::convert::From<BoxBody>` is not implemented for `actix_web::dev::Response<_>`
|
= help: the following other types implement trait `std::convert::From<T>`:
<actix_web::dev::Response<&'static [u8]> as std::convert::From<&'static [u8]>>
<actix_web::dev::Response<&'static str> as std::convert::From<&'static str>>
<actix_web::dev::Response<B> as std::convert::From<HttpResponse<B>>>
<actix_web::dev::Response<B> as std::convert::From<ServiceResponse<B>>>
<actix_web::dev::Response<BoxBody> as std::convert::From<&actix_http::ws::HandshakeError>>
<actix_web::dev::Response<BoxBody> as std::convert::From<HttpResponseBuilder>>
<actix_web::dev::Response<BoxBody> as std::convert::From<Infallible>>
<actix_web::dev::Response<BoxBody> as std::convert::From<Result<I, E>>>
and 13 others
= note: required because of the requirements on the impl of `Into<actix_web::dev::Response<_>>` for `BoxBody`
note: required by a bound in `ServiceRequest::into_response`
--> /Users/nshv/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.2.1/src/service.rs:156:32
|
156 | pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
| ^^^^^^^^^^^^^^^^^ required by this bound in `ServiceRequest::into_response`
error[E0277]: the trait bound `Cors: actix_web::dev::Transform<actix_web::app_service::AppRouting, ServiceRequest>` is not satisfied
--> src/main.rs:60:17
|
59 | .wrap(
| ---- required by a bound introduced by this call
60 | / Cors::default() // allowed_origin return access-control-allow-origin: * by default
61 | | .allowed_origin("http://127.0.0.1:3000")
62 | | .allowed_origin("http://localhost:3000")
63 | | .send_wildcard()
... |
66 | | .allowed_header(http::header::CONTENT_TYPE)
67 | | .max_age(3600),
| |__________________________________^ the trait `actix_web::dev::Transform<actix_web::app_service::AppRouting, ServiceRequest>` is not implemented for `Cors`
|
= help: the following other types implement trait `actix_web::dev::Transform<S, Req>`:
<Arc<T> as actix_web::dev::Transform<S, Req>>
<Compress as actix_web::dev::Transform<S, ServiceRequest>>
<Condition<T> as actix_web::dev::Transform<S, Req>>
<DefaultHeaders as actix_web::dev::Transform<S, ServiceRequest>>
<ErrorHandlers<B> as actix_web::dev::Transform<S, ServiceRequest>>
<NormalizePath as actix_web::dev::Transform<S, ServiceRequest>>
<Rc<T> as actix_web::dev::Transform<S, Req>>
<actix_service::transform_err::TransformMapInitErr<T, S, Req, F, E> as actix_web::dev::Transform<S, Req>>
and 2 others
note: required by a bound in `App::<T>::wrap`
--> /Users/nshv/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.2.1/src/app.rs:364:12
|
364 | M: Transform<
| ____________^
365 | | T::Service,
366 | | ServiceRequest,
367 | | Response = ServiceResponse<B>,
368 | | Error = Error,
369 | | InitError = (),
370 | | > + 'static,
| |_____________^ required by this bound in `App::<T>::wrap`
warning: use of deprecated associated function `actix_web::App::<T>::data`: Use `.app_data(Data::new(val))` instead.
--> src/main.rs:69:14
|
69 | .data(pool.clone())
| ^^^^
|
= note: `#[warn(deprecated)]` on by default
error[E0277]: the trait bound `Authentication: actix_web::dev::Transform<<impl actix_web::dev::ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse<actix
_web::middleware::logger::StreamLog<_>>, Error = actix_web::Error, InitError = ()> as actix_web::dev::ServiceFactory<ServiceRequest>>::Service, ServiceRequest>` is not sat
isfied
--> src/main.rs:71:19
|
71 | .wrap(crate::middleware::auth_middleware::Authentication) // Comment this line of code if you want to integrate with yew-addr...
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `actix_web::dev::Transform<<impl actix_web::dev::ServiceFactory<ServiceRequest, Config
= (), Response = ServiceResponse<actix_web::middleware::logger::StreamLog<_>>, Error = actix_web::Error, InitError = ()> as actix_web::dev::ServiceFactory<ServiceRequest>
>::Service, ServiceRequest>` is not implemented for `Authentication`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `actix_web::dev::Transform<S, Req>`:
<Arc<T> as actix_web::dev::Transform<S, Req>>
<Compress as actix_web::dev::Transform<S, ServiceRequest>>
<Condition<T> as actix_web::dev::Transform<S, Req>>
<DefaultHeaders as actix_web::dev::Transform<S, ServiceRequest>>
<ErrorHandlers<B> as actix_web::dev::Transform<S, ServiceRequest>>
<NormalizePath as actix_web::dev::Transform<S, ServiceRequest>>
<Rc<T> as actix_web::dev::Transform<S, Req>>
<actix_service::transform_err::TransformMapInitErr<T, S, Req, F, E> as actix_web::dev::Transform<S, Req>>
and 2 others
note: required by a bound in `App::<T>::wrap`
--> /Users/nshv/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.2.1/src/app.rs:364:12
|
364 | M: Transform<
| ____________^
365 | | T::Service,
366 | | ServiceRequest,
367 | | Response = ServiceResponse<B>,
368 | | Error = Error,
369 | | InitError = (),
370 | | > + 'static,
| |_____________^ required by this bound in `App::<T>::wrap`
error[E0599]: the method `call` exists for reference `&<impl actix_web::dev::ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse<_>, Error = actix_web::
Error, InitError = ()> as actix_web::dev::ServiceFactory<ServiceRequest>>::Service`, but its trait bounds were not satisfied
--> src/main.rs:72:37
|
72 | .wrap_fn(|req, srv| srv.call(req).map(|res| res))
| ^^^^ method cannot be called on `&<impl actix_web::dev::ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse<_>,
Error = actix_web::Error, InitError = ()> as actix_web::dev::ServiceFactory<ServiceRequest>>::Service` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`<impl actix_web::dev::ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse<_>, Error = actix_web::Error, InitError = ()> as actix_web::dev::S
erviceFactory<ServiceRequest>>::Service: Fn<_>`
which is required by `&<impl actix_web::dev::ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse<_>, Error = actix_web::Error, InitError = ()
> as actix_web::dev::ServiceFactory<ServiceRequest>>::Service: Fn<_>`
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
3 | use crate::actix_web::dev::Service;
|
warning: unused import: `futures::FutureExt`
--> src/main.rs:40:5
|
40 | use futures::FutureExt;
| ^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unused import: `actix_service::Service`
--> src/main.rs:38:5
|
38 | use actix_service::Service;
| ^^^^^^^^^^^^^^^^^^^^^^
Some errors have detailed explanations: E0277, E0432, E0599.
For more information about an error, try `rustc --explain E0277`.
warning: `actix-web-rest-api-with-jwt` (bin "actix-web-rest-api-with-jwt") generated 3 warnings
error: could not compile `actix-web-rest-api-with-jwt` due to 6 previous errors; 3 warnings emitted
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.