Created
October 21, 2019 14:38
-
-
Save trimoq/ea008739febd0bc734ca4abbb25df3ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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