Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
impl<'a, 'r> FromRequest<'a, 'r> for AuthUser {
type Error = ApiError;
fn from_request(request: &'a Request<'r>) -> request::Outcome<AuthUser, Self::Error> {
let con = request.guard::<DbConn>().unwrap();
request.cookies()
.get_private("user_id")
.and_then(|cookie| cookie.value().parse().ok())
.and_then(|id| ApiUserDAO::get_by_slug_enabled(&con,id))
.or_else( || {
request.headers().get_one("x-api-key")
.and_then(|header| TokenDAO::get_user_for_token_or_err(&con,header.to_string()).ok())
})
.into_outcome((Status::Unauthorized,ApiError::unauthorized("unauthorized")))
.map(|user| AuthUser(user))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment