1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-12-04 20:11:55 +01:00
actix-extras/actix-web-httpauth/examples/with-cors.rs

32 lines
886 B
Rust
Raw Normal View History

use actix_cors::Cors;
use actix_web::{dev::ServiceRequest, get, App, Error, HttpResponse, HttpServer};
2021-08-31 00:27:44 +02:00
use actix_web_httpauth::{extractors::bearer::BearerAuth, middleware::HttpAuthentication};
async fn ok_validator(
req: ServiceRequest,
credentials: BearerAuth,
) -> Result<ServiceRequest, Error> {
eprintln!("{:?}", credentials);
Ok(req)
}
#[get("/")]
async fn index() -> HttpResponse {
HttpResponse::Ok().finish()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.wrap(HttpAuthentication::bearer(ok_validator))
// ensure the CORS middleware is wrapped around the httpauth middleware so it is able to
// add headers to error responses
.wrap(Cors::permissive())
.service(index)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}