Skip to content

Instantly share code, notes, and snippets.

@trimoq
Created October 21, 2019 09:18
Show Gist options
  • Save trimoq/860c3b2b94d6711905cae81dd8818093 to your computer and use it in GitHub Desktop.
Save trimoq/860c3b2b94d6711905cae81dd8818093 to your computer and use it in GitHub Desktop.
Get the username from the cookie
impl<'a, 'r> FromRequest<'a, 'r> for AuthUser {
type Error = ApiError;
fn from_request(request: &'a Request<'r>) -> request::Outcome<AuthUser, Self::Error> {
request.cookies()
.get_private("user_id")
.and_then(|cookie| cookie.value().parse().ok())
.into_outcome((Status::Unauthorized,ApiError::unauthorized("Unauthorized or no cookie")))
.map(|user| AuthUser(user))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment