Reputation: 93
I am writing a play application in which I have 2 functions as follows
And then my refine fn as shown below
def refine[A](request: Request[A]) = {
val jwtToken = request.headers.get("authorization").getOrElse("")
val currentUser = for {
json <- OptionT.fromOption[Future]( jwtUtils.decodePayload(jwtToken).toOption )
user <- OptionT( userRepo.find((json \ "sub").as[String].toLong) )
} yield user
currentUser.value match {
case fou : Future[Option[User]] => {
fou.map { ou =>
ou match {
case Some(user) => Right(new UserRequest(user, request))
case _ => Left(Forbidden)
}
}
}
case _ => Future.successful( Left(Forbidden) )
}
}
The refine() fn needs to return a Future[Either]. Is there a better way to write the second half of the refine fn to do that ?
Upvotes: 3
Views: 850
Reputation: 1054
Seems like you can use Either.fromOption
from cats library.
currentUser.value.map(opt =>
Either.fromOption(opt.map(new UserRequest(_, request)), Forbidden))
Upvotes: 2