I made a Actix Webserver which includes a scope ("/api") with a few routes.
Now I want to expose the API but I want to limit the access via tokens and I found a way to do it by getting the body in a route and checking if the token is correct ...
#[post("/test")]
async fn write_content(req_body: String) -> impl Responder {
let body_str = req_body.to_string();
let body: WriteContentBody = serde_json::from_str(&body_str).unwrap();
// of course I will make it in the future with a db ... this is just for getting the basics
if body.token === "aaaaaaaaaaaaaaaa" {
HttpResponse::Ok().body("200")
} else {
HttpResponse::Ok().body("401")
}
}
but I basically want to do it for my whole scope which includes a few routes without making this check in every route.
something like
App::new()
.service(
web::scope("/api")
.before_check(token_check)
.service(index)
.service(test)
.service(get_content)
.service(write_content)
.service(new_project)
.service(list_projects)
)