Is there a neater way of handling the option response from req.cookie()
in this code block (line 3 onwards)? Id like to avoid the nested if statements, and multiple returns if at all possible. req.cookie()
returns an Option<Cookie>
and the Cookie
struct has a value function. If the cookie is present but has the wrong value, i want to return a bad request, however i also want to return a bad request if the cookie is not present at all. If the cookie value is OK, i want to continue and do further work:
#[get("/callback")]
async fn callback(req: HttpRequest, client: web::Data<BasicClient>, web::Query(cb): web::Query<Callback>) -> Result<HttpResponse> {
if let Some(state_cookie) = req.cookie(STATE_COOKIE_NAME) {
if state_cookie.value() != cb.state {
return Ok(HttpResponse::BadRequest().finish())
}
} else {
return Ok(HttpResponse::BadRequest().finish())
}
let token_result = client.exchange_code(AuthorizationCode::new(cb.code)).request(http_client);
return match token_result {
Ok(token_details) => Ok(handle_success(token_details)),
Err(token_error) => Ok(handle_error(token_error))
}
}