I currently have a Login struct (email, password) and an associated request guard that checks if the needed email and password headers are in the request. If so the struct is built and passed to the route if not a 401, Bad Request
is returned.
MY GOAL:
Instead of 'no parameters' i would like to indicate using 2 fields that 2 headers are required for this request to be valid.
In the rocket_okapi = { version = "0.8.0-rc.2", features = ["swagger", "rapidoc"]}
i see what can be a solution but I cannot understand how to add the multiple headers i need.
the swagger UI in the image above is generated by the
#[openapi(tag = "Acess")]
at the start of the/login
route in my rust code.
pub struct LoginUser {
email: String,
password: String,
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for LoginUser {
type Error = String;
async fn from_request(request: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let (email, password) = match (
request.headers().get_one("email"),
request.headers().get_one("password"),
) {
(Some(email), Some(password)) => (email, password),
_ => {
return Outcome::Failure((
Status::BadRequest,
"Missing email and/or password headers".into(),
))
}
};
let user = LoginUser {
email: email.to_string(),
password: match hash(password.to_string(), 10) {
Ok(hashed_password) => hashed_password,
Err(_) => return Outcome::Failure((Status::InternalServerError, "".into())),
},
};
Outcome::Success(user)
}
}
#[openapi(tag = "Acess")]
#[post("/login")]
pub async fn login(
conn: crate::database::DbConn,
user: LoginUser,
) {
<Verify the user is in the Database>
}