我正在使用 sqlx 在 actix-web 2.0.0 中实现身份验证提取器来访问数据库。我有这个代码:
use actix_web::{dev, web, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorUnauthorized;
use futures::future::{ok, err, Ready};
use sqlx::PgPool;
use serde_derive::Deserialize;
use crate::model::User;
#[derive(Debug, Deserialize)]
pub struct Auth {
user_id: u32,
}
impl FromRequest for Auth {
type Error = Error;
type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut dev::Payload) -> Self::Future {
use actix_web::HttpMessage;
let db_pool = req.app_data::<web::Data<PgPool>>().unwrap();
let error = ErrorUnauthorized("{\"details\": \"Please log in\"}");
if let Some(session_id) = req.cookie("sessionid") {
log::info!("Session id {}", session_id);
// let result = User::find_by_session(db_pool.get_ref(), session_id).await;
ok(Auth { user_id: 0 })
} else {
err(error)
}
}
}
当然,我不能await
在那里使用。我看到了一个使用type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>
和返回的例子,Box::pin(async move { ... })
但我无法让它工作(有生命周期的问题req
)。