Skip to content

Instantly share code, notes, and snippets.

@vkostyukov
Last active October 17, 2015 17:57
Show Gist options
  • Save vkostyukov/0ca4ee48920c2e39c442 to your computer and use it in GitHub Desktop.
Save vkostyukov/0ca4ee48920c2e39c442 to your computer and use it in GitHub Desktop.
Finch Auth
def auth0[A, B](doAuth: Request => Future[A])(e: Endpoint[B])(implicit
adjoin: PairAdjoin[A, B]
): Endpoint[adjoin.Out] = new Endpoint[adjoin.Out] {
def apply(input: Input): Option[(Input, () => Future[Output[adjoin.Out]])] =
e(input).map {
case (remainder, output) =>
(remainder, () => for {
a <- doAuth(input.request)
ob <- output()
} yield b.copy(value = adjoin(a, b.value)))
}
}
case class User(id: Int)
val auth: Endpoint[A] => Endpoint[User :: A :: HNil] = auth0(req => Future.value(User(1)))
// Usage
auth(put("users" / int)) { (u: User, id: Int) =>
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment