Created
February 10, 2020 12:49
-
-
Save Algiras/8e3582de7b0c1a71dc893bf7ec72c069 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
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