Skip to content

Instantly share code, notes, and snippets.

@Algiras
Created February 10, 2020 12:49
Show Gist options
  • Save Algiras/8e3582de7b0c1a71dc893bf7ec72c069 to your computer and use it in GitHub Desktop.
Save Algiras/8e3582de7b0c1a71dc893bf7ec72c069 to your computer and use it in GitHub Desktop.
implicit val loginUserDecoder: Decoder[UsernamePasswordCredentials] = deriveDecoder
implicit val entityLoginUserDecoder: EntityDecoder[IO, UsernamePasswordCredentials] =
jsonOf[IO, UsernamePasswordCredentials]
def loginRoute(
auth: BearerTokenAuthenticator[IO, UserId, User],
checkPassword: UsernamePasswordCredentials => IO[Option[User]]): HttpRoutes[IO] =
HttpRoutes.of[IO] {
case req @ POST -> Root / "login" =>
(for {
user <- req.as[UsernamePasswordCredentials]
userOpt <- checkPassword(user)
} yield userOpt).flatMap {
case Some(user) => auth.create(user.id).map(auth.embed(Response(Status.Ok), _))
case None => IO.pure(Response[IO](Status.Unauthorized))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment